blob: c4072fd80307edb6c11789c03e46a15bf1339cba [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000017#include "clang/AST/CharUnits.h"
Zhongxing Xuc5063572010-03-16 13:14:16 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Checker/PathSensitive/GRState.h"
24#include "clang/Checker/PathSensitive/GRStateTrait.h"
25#include "clang/Checker/PathSensitive/MemRegion.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Ted Kremenek66d51422010-04-09 20:26:58 +000027#include "llvm/ADT/ImmutableMap.h"
28#include "llvm/ADT/Optional.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000029#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000030
31using namespace clang;
Ted Kremenek66d51422010-04-09 20:26:58 +000032using llvm::Optional;
Zhongxing Xu17892752008-10-08 02:50:44 +000033
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000040public:
Ted Kremeneke393f4a2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000044 uint64_t Offset;
45
Ted Kremeneke393f4a2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Zhongxing Xubfcaf802010-02-05 02:26:30 +000047 : P(r, (unsigned) k), Offset(offset) { assert(r); }
Ted Kremeneke393f4a2010-02-03 03:06:46 +000048public:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000049
Ted Kremeneke393f4a2010-02-03 03:06:46 +000050 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000052
Ted Kremeneke393f4a2010-02-03 03:06:46 +000053 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000055
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000056 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000057 ID.AddPointer(P.getOpaqueValue());
58 ID.AddInteger(Offset);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000059 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000060
Ted Kremeneke393f4a2010-02-03 03:06:46 +000061 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000062
Ted Kremeneke393f4a2010-02-03 03:06:46 +000063 bool operator<(const BindingKey &X) const {
64 if (P.getOpaqueValue() < X.P.getOpaqueValue())
65 return true;
66 if (P.getOpaqueValue() > X.P.getOpaqueValue())
67 return false;
68 return Offset < X.Offset;
69 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000070
Ted Kremeneke393f4a2010-02-03 03:06:46 +000071 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000075};
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000076} // end anonymous namespace
77
78namespace llvm {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +000079 static inline
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000080 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +000081 os << '(' << K.getRegion() << ',' << K.getOffset()
82 << ',' << (K.isDirect() ? "direct" : "default")
83 << ')';
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000084 return os;
85 }
86} // end llvm namespace
87
88//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +000089// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000090//===----------------------------------------------------------------------===//
91
Ted Kremeneke393f4a2010-02-03 03:06:46 +000092typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000093
Ted Kremenek50dc1b32008-12-24 01:05:03 +000094//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000095// Fine-grained control of RegionStoreManager.
96//===----------------------------------------------------------------------===//
97
98namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000099struct minimal_features_tag {};
100struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000102class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 bool SupportsFields;
104 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek9af46f52009-06-16 22:36:44 +0000106public:
107 RegionStoreFeatures(minimal_features_tag) :
108 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek9af46f52009-06-16 22:36:44 +0000110 RegionStoreFeatures(maximal_features_tag) :
111 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9af46f52009-06-16 22:36:44 +0000113 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek9af46f52009-06-16 22:36:44 +0000115 bool supportsFields() const { return SupportsFields; }
116 bool supportsRemaining() const { return SupportsRemaining; }
117};
118}
119
120//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000121// Region "Extents"
122//===----------------------------------------------------------------------===//
123//
124// MemRegions represent chunks of memory with a size (their "extent"). This
125// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000126//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000127namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000128static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000129namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000130 template<> struct GRStateTrait<RegionExtents>
131 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
132 static void* GDMIndex() { return &RegionExtentsIndex; }
133 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000134}
135
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000136//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000137// Utility functions.
138//===----------------------------------------------------------------------===//
139
140static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
141 if (ty->isAnyPointerType())
142 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 return ty->isIntegerType() && ty->isScalarType() &&
145 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
146}
147
148//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000149// Main RegionStore logic.
150//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000151
Zhongxing Xu17892752008-10-08 02:50:44 +0000152namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000154class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekdf165012010-02-02 22:38:47 +0000155public:
156 typedef llvm::ImmutableSet<const MemRegion*> Set;
157 typedef llvm::DenseMap<const MemRegion*, Set> Map;
158private:
159 Set::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000161public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000162 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000164
165 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000166 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000167 return true;
168 }
169
170 I->second = F.Add(I->second, SubRegion);
171 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000174 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 ~RegionStoreSubRegionMap() {}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000177
Ted Kremenekdf165012010-02-02 22:38:47 +0000178 const Set *getSubRegions(const MemRegion *Parent) const {
179 Map::const_iterator I = M.find(Parent);
180 return I == M.end() ? NULL : &I->second;
181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenek5dc27462009-03-03 02:51:43 +0000183 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000184 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000185
186 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000187 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenekdf165012010-02-02 22:38:47 +0000189 Set S = I->second;
190 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000191 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000192 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek5dc27462009-03-03 02:51:43 +0000195 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000198
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000199
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000200class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000202 RegionBindings::Factory RBFactory;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000203
Zhongxing Xu17892752008-10-08 02:50:44 +0000204public:
Mike Stump1eb44332009-09-09 15:08:12 +0000205 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000206 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000207 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000208 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000209
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000210 SubRegionMap *getSubRegionMap(Store store) {
211 return getRegionStoreSubRegionMap(store);
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Zhongxing Xu13d50172009-10-11 08:08:02 +0000214 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Zhongxing Xu13d50172009-10-11 08:08:02 +0000216 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
217 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000218 /// getDefaultBinding - Returns an SVal* representing an optional default
219 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000220 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000221
Ted Kremenek027e2662009-11-19 20:20:24 +0000222 /// setImplicitDefaultValue - Set the default binding for the provided
223 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000224 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000225 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000227 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
228 /// type. 'Array' represents the lvalue of the array being decayed
229 /// to a pointer, and the returned SVal represents the decayed
230 /// version of that lvalue (i.e., a pointer to the first element of
231 /// the array). This is called by GRExprEngine when evaluating
232 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000233 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000234
Zhongxing Xu461147f2010-02-05 05:24:20 +0000235 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000236
Mike Stump1eb44332009-09-09 15:08:12 +0000237 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000238 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000239 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000240
Ted Kremenek67f28532009-06-17 22:02:04 +0000241 //===-------------------------------------------------------------------===//
242 // Binding values to regions.
243 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000244
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000245 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000246 unsigned Count, InvalidatedSymbols *IS) {
247 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS);
Ted Kremenek81a95832009-12-03 03:27:11 +0000248 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000249
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000250 Store InvalidateRegions(Store store,
251 const MemRegion * const *Begin,
252 const MemRegion * const *End,
253 const Expr *E, unsigned Count,
254 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000256public: // Made public for helper classes.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000257
Zhongxing Xu13d50172009-10-11 08:08:02 +0000258 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000259 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000261 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
262
263 RegionBindings Add(RegionBindings B, const MemRegion *R,
264 BindingKey::Kind k, SVal V);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000265
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000266 const SVal *Lookup(RegionBindings B, BindingKey K);
267 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000268
269 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000270 RegionBindings Remove(RegionBindings B, const MemRegion *R,
271 BindingKey::Kind k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000272
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000273 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
274 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000275 }
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000276
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000277 Store Remove(Store store, BindingKey K);
278
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000279public: // Part of public interface to class.
280
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000281 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000282
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000283 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
284 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000286 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000287
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000288 Store BindDeclWithNoInit(Store store, const VarRegion *) {
289 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000290 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000291
Ted Kremenek67f28532009-06-17 22:02:04 +0000292 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000293 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000295 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
297 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000298 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 Store Remove(Store store, Loc LV);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000301
Ted Kremenek67f28532009-06-17 22:02:04 +0000302
303 //===------------------------------------------------------------------===//
304 // Loading values from regions.
305 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenek67f28532009-06-17 22:02:04 +0000307 /// The high level logic for this method is this:
308 /// Retrieve (L)
309 /// if L has binding
310 /// return L's binding
311 /// else if L is in killset
312 /// return unknown
313 /// else
314 /// if L is on stack or heap
315 /// return undefined
316 /// else
317 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000318 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000319
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000320 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000321
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000322 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Zhongxing Xu576bb922010-02-05 03:01:53 +0000324 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Zhongxing Xu576bb922010-02-05 03:01:53 +0000326 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Zhongxing Xu576bb922010-02-05 03:01:53 +0000328 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000330 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000331 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek67f28532009-06-17 22:02:04 +0000333 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000334 /// struct copy:
335 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000336 /// x = y;
337 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000338 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Zhongxing Xu576bb922010-02-05 03:01:53 +0000340 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000342 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000343 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000344 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000346 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
347 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000348
349 //===------------------------------------------------------------------===//
350 // State pruning.
351 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
354 /// It returns a new Store with these values removed.
Zhongxing Xu95798982010-05-26 03:27:35 +0000355 const GRState *RemoveDeadBindings(GRState &state, Stmt* Loc,
356 const StackFrameContext *LCtx,
357 SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000358 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
359
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000360 const GRState *EnterStackFrame(const GRState *state,
361 const StackFrameContext *frame);
362
Ted Kremenek67f28532009-06-17 22:02:04 +0000363 //===------------------------------------------------------------------===//
364 // Region "extents".
365 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Zhongxing Xuab280992010-05-25 04:59:19 +0000367 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent){
368 return state->set<RegionExtents>(R, Extent);
369 }
370
371 Optional<SVal> getExtent(const GRState *state, const MemRegion *R) {
372 const SVal *V = state->get<RegionExtents>(R);
373 if (V)
374 return *V;
375 else
376 return Optional<SVal>();
377 }
378
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000379 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000380 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000381
382 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000383 // Utility methods.
384 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek451ac092009-08-06 04:50:20 +0000386 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000387 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000388 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000389
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000390 void print(Store store, llvm::raw_ostream& Out, const char* nl,
391 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000392
393 void iterBindings(Store store, BindingsHandler& f) {
394 // FIXME: Implement.
395 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000396
Ted Kremenek67f28532009-06-17 22:02:04 +0000397 // FIXME: Remove.
398 BasicValueFactory& getBasicVals() {
399 return StateMgr.getBasicVals();
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek67f28532009-06-17 22:02:04 +0000402 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000403 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000404};
405
406} // end anonymous namespace
407
Ted Kremenek9af46f52009-06-16 22:36:44 +0000408//===----------------------------------------------------------------------===//
409// RegionStore creation.
410//===----------------------------------------------------------------------===//
411
412StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
413 RegionStoreFeatures F = maximal_features_tag();
414 return new RegionStoreManager(StMgr, F);
415}
416
417StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
418 RegionStoreFeatures F = minimal_features_tag();
419 F.enableFields(true);
420 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000421}
422
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000423void
424RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000425 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000426 const MemRegion *superR = R->getSuperRegion();
427 if (add(superR, R))
428 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000429 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430}
431
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000432RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000433RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
434 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000435 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437 llvm::SmallVector<const SubRegion*, 10> WL;
438
Ted Kremenek451ac092009-08-06 04:50:20 +0000439 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000440 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000441 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Mike Stump1eb44332009-09-09 15:08:12 +0000443 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444 // don't have direct bindings but are super regions of those that do.
445 while (!WL.empty()) {
446 const SubRegion *R = WL.back();
447 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000448 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000449 }
450
Ted Kremenek14453bf2009-03-03 19:02:42 +0000451 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000452}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000453
Ted Kremenek9af46f52009-06-16 22:36:44 +0000454//===----------------------------------------------------------------------===//
Ted Kremeneka4fab032010-03-10 07:19:59 +0000455// Region Cluster analysis.
456//===----------------------------------------------------------------------===//
457
458namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000459template <typename DERIVED>
Ted Kremeneka4fab032010-03-10 07:19:59 +0000460class ClusterAnalysis {
461protected:
462 typedef BumpVector<BindingKey> RegionCluster;
463 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5499b842010-03-10 16:32:56 +0000464 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
465 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
466 WorkList;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000467
468 BumpVectorContext BVC;
469 ClusterMap ClusterM;
Ted Kremenek5499b842010-03-10 16:32:56 +0000470 WorkList WL;
Ted Kremeneka4fab032010-03-10 07:19:59 +0000471
472 RegionStoreManager &RM;
473 ASTContext &Ctx;
474 ValueManager &ValMgr;
475
Ted Kremenek5499b842010-03-10 16:32:56 +0000476 RegionBindings B;
477
Ted Kremeneka4fab032010-03-10 07:19:59 +0000478public:
Ted Kremenek5499b842010-03-10 16:32:56 +0000479 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
480 RegionBindings b)
481 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
482 B(b) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000483
Ted Kremenek5499b842010-03-10 16:32:56 +0000484 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000485
Ted Kremenek5499b842010-03-10 16:32:56 +0000486 void AddToCluster(BindingKey K) {
487 const MemRegion *R = K.getRegion();
488 const MemRegion *baseR = R->getBaseRegion();
489 RegionCluster &C = getCluster(baseR);
490 C.push_back(K, BVC);
491 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000492 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000493
Ted Kremenek5499b842010-03-10 16:32:56 +0000494 bool isVisited(const MemRegion *R) {
495 return (bool) Visited[&getCluster(R->getBaseRegion())];
496 }
497
498 RegionCluster& getCluster(const MemRegion *R) {
499 RegionCluster *&CRef = ClusterM[R];
500 if (!CRef) {
501 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
502 CRef = new (Mem) RegionCluster(BVC, 10);
503 }
504 return *CRef;
505 }
506
507 void GenerateClusters() {
508 // Scan the entire set of bindings and make the region clusters.
509 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
510 AddToCluster(RI.getKey());
511 if (const MemRegion *R = RI.getData().getAsRegion()) {
512 // Generate a cluster, but don't add the region to the cluster
513 // if there aren't any bindings.
514 getCluster(R->getBaseRegion());
515 }
Ted Kremeneka4fab032010-03-10 07:19:59 +0000516 }
517 }
Ted Kremenek5499b842010-03-10 16:32:56 +0000518
519 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
520 if (unsigned &visited = Visited[&C])
521 return false;
522 else
523 visited = 1;
524
525 WL.push_back(std::make_pair(R, &C));
526 return true;
527 }
528
529 bool AddToWorkList(BindingKey K) {
530 return AddToWorkList(K.getRegion());
531 }
532
533 bool AddToWorkList(const MemRegion *R) {
534 const MemRegion *baseR = R->getBaseRegion();
535 return AddToWorkList(baseR, getCluster(baseR));
536 }
537
538 void RunWorkList() {
539 while (!WL.empty()) {
540 const MemRegion *baseR;
541 RegionCluster *C;
542 llvm::tie(baseR, C) = WL.back();
543 WL.pop_back();
544
545 // First visit the cluster.
546 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
547
Ted Kremenek75a2d942010-04-01 00:15:55 +0000548 // Next, visit the base region.
549 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenek5499b842010-03-10 16:32:56 +0000550 }
551 }
552
553public:
554 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
555 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenek75a2d942010-04-01 00:15:55 +0000556 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenek5499b842010-03-10 16:32:56 +0000557};
Ted Kremeneka4fab032010-03-10 07:19:59 +0000558}
559
560//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000561// Binding invalidation.
562//===----------------------------------------------------------------------===//
563
Zhongxing Xu13d50172009-10-11 08:08:02 +0000564void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
565 const MemRegion *R,
566 RegionStoreSubRegionMap &M) {
Ted Kremeneka4fab032010-03-10 07:19:59 +0000567
Ted Kremenekdf165012010-02-02 22:38:47 +0000568 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
569 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
570 I != E; ++I)
571 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000572
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000573 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000574}
575
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000576namespace {
Ted Kremenek5499b842010-03-10 16:32:56 +0000577class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
578{
579 const Expr *Ex;
580 unsigned Count;
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000581 StoreManager::InvalidatedSymbols *IS;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000582public:
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000583 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenek5499b842010-03-10 16:32:56 +0000584 GRStateManager &stateMgr,
585 RegionBindings b,
586 const Expr *ex, unsigned count,
587 StoreManager::InvalidatedSymbols *is)
588 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
589 Ex(ex), Count(count), IS(is) {}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000590
Ted Kremenek5499b842010-03-10 16:32:56 +0000591 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek75a2d942010-04-01 00:15:55 +0000592 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000593
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000594private:
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000595 void VisitBinding(SVal V);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000596};
Ted Kremenek5b290652010-02-03 04:16:00 +0000597}
598
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000599void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenekc1ddcab2010-02-13 00:54:03 +0000600 // A symbol? Mark it touched by the invalidation.
601 if (IS)
602 if (SymbolRef Sym = V.getAsSymbol())
603 IS->insert(Sym);
Ted Kremeneka4fab032010-03-10 07:19:59 +0000604
Ted Kremenek24c37ad2010-02-13 01:52:33 +0000605 if (const MemRegion *R = V.getAsRegion()) {
606 AddToWorkList(R);
607 return;
608 }
609
610 // Is it a LazyCompoundVal? All references get invalidated as well.
611 if (const nonloc::LazyCompoundVal *LCS =
612 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
613
614 const MemRegion *LazyR = LCS->getRegion();
615 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
616
617 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
618 const MemRegion *baseR = RI.getKey().getRegion();
619 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
620 VisitBinding(RI.getData());
621 }
622
623 return;
624 }
625}
626
Ted Kremenek5499b842010-03-10 16:32:56 +0000627void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
628 BindingKey *I, BindingKey *E) {
629 for ( ; I != E; ++I) {
630 // Get the old binding. Is it a region? If so, add it to the worklist.
631 const BindingKey &K = *I;
632 if (const SVal *V = RM.Lookup(B, K))
633 VisitBinding(*V);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000634
Ted Kremenek5499b842010-03-10 16:32:56 +0000635 B = RM.Remove(B, K);
636 }
637}
Ted Kremeneka4fab032010-03-10 07:19:59 +0000638
Ted Kremenek75a2d942010-04-01 00:15:55 +0000639void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000640 if (IS) {
641 // Symbolic region? Mark that symbol touched by the invalidation.
642 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
643 IS->insert(SR->getSymbol());
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000644 }
645
Ted Kremenek5499b842010-03-10 16:32:56 +0000646 // BlockDataRegion? If so, invalidate captured variables that are passed
647 // by reference.
648 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
649 for (BlockDataRegion::referenced_vars_iterator
650 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
651 BI != BE; ++BI) {
652 const VarRegion *VR = *BI;
653 const VarDecl *VD = VR->getDecl();
654 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
655 AddToWorkList(VR);
656 }
657 return;
658 }
659
660 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
661 // Invalidate the region by setting its default value to
662 // conjured symbol. The type of the symbol is irrelavant.
663 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
664 Count);
665 B = RM.Add(B, baseR, BindingKey::Default, V);
666 return;
667 }
668
669 if (!baseR->isBoundable())
670 return;
671
672 const TypedRegion *TR = cast<TypedRegion>(baseR);
673 QualType T = TR->getValueType(Ctx);
674
675 // Invalidate the binding.
676 if (const RecordType *RT = T->getAsStructureType()) {
677 const RecordDecl *RD = RT->getDecl()->getDefinition();
678 // No record definition. There is nothing we can do.
679 if (!RD) {
680 B = RM.Remove(B, baseR);
681 return;
682 }
683
684 // Invalidate the region by setting its default value to
685 // conjured symbol. The type of the symbol is irrelavant.
686 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
687 Count);
688 B = RM.Add(B, baseR, BindingKey::Default, V);
689 return;
690 }
691
692 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
693 // Set the default value of the array to conjured symbol.
694 DefinedOrUnknownSVal V =
695 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
696 B = RM.Add(B, baseR, BindingKey::Default, V);
697 return;
698 }
699
700 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
701 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
702 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000703}
704
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000705Store RegionStoreManager::InvalidateRegions(Store store,
706 const MemRegion * const *I,
707 const MemRegion * const *E,
708 const Expr *Ex, unsigned Count,
709 InvalidatedSymbols *IS) {
Ted Kremenek5499b842010-03-10 16:32:56 +0000710 InvalidateRegionsWorker W(*this, StateMgr,
711 RegionStoreManager::GetRegionBindings(store),
712 Ex, Count, IS);
713
714 // Scan the bindings and generate the clusters.
715 W.GenerateClusters();
716
717 // Add I .. E to the worklist.
718 for ( ; I != E; ++I)
719 W.AddToWorkList(*I);
720
721 W.RunWorkList();
722
723 // Return the new bindings.
724 return W.getRegionBindings().getRoot();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000725}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000726
Ted Kremenek9af46f52009-06-16 22:36:44 +0000727//===----------------------------------------------------------------------===//
728// Extents for regions.
729//===----------------------------------------------------------------------===//
730
Zhongxing Xue884ff82009-11-12 02:48:32 +0000731DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000732 const MemRegion *R,
733 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000735 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000736 case MemRegion::CXXThisRegionKind:
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000737 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000738 case MemRegion::GenericMemSpaceRegionKind:
739 case MemRegion::StackLocalsSpaceRegionKind:
740 case MemRegion::StackArgumentsSpaceRegionKind:
741 case MemRegion::HeapSpaceRegionKind:
742 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000743 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000744 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000745 return UnknownVal();
746
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000747 case MemRegion::FunctionTextRegionKind:
748 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000749 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000750 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000751 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000752
753 // Not yet handled.
754 case MemRegion::AllocaRegionKind:
755 case MemRegion::CompoundLiteralRegionKind:
756 case MemRegion::ElementRegionKind:
757 case MemRegion::FieldRegionKind:
758 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000759 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000760 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000762 case MemRegion::SymbolicRegionKind: {
763 const SVal *Size = state->get<RegionExtents>(R);
764 if (!Size)
765 return UnknownVal();
766 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
767 if (!CI)
768 return UnknownVal();
769
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000770 CharUnits RegionSize =
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000771 CharUnits::fromQuantity(CI->getValue().getSExtValue());
772 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
773 assert(RegionSize % EleSize == 0);
774
775 return ValMgr.makeIntVal(RegionSize / EleSize, false);
776 }
777
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000778 case MemRegion::StringRegionKind: {
779 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000780 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000781 // operations with signed indices.
782 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000785 case MemRegion::VarRegionKind: {
786 const VarRegion* VR = cast<VarRegion>(R);
787 // Get the type of the variable.
788 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000790 // FIXME: Handle variable-length arrays.
791 if (isa<VariableArrayType>(T))
792 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000794 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
795 // return the size as signed integer.
796 return ValMgr.makeIntVal(CAT->getSize(), false);
797 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000798
Zhongxing Xu9618b852010-04-01 08:20:27 +0000799 // Clients can reinterpret ordinary variables as arrays, possibly of
800 // another type. The width is rounded down to ensure that an access is
801 // entirely within bounds.
802 CharUnits VarSize = getContext().getTypeSizeInChars(T);
803 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
804 return ValMgr.makeIntVal(VarSize / EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000805 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000808 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000809 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000810}
811
Ted Kremenek9af46f52009-06-16 22:36:44 +0000812//===----------------------------------------------------------------------===//
813// Location and region casting.
814//===----------------------------------------------------------------------===//
815
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000816/// ArrayToPointer - Emulates the "decay" of an array to a pointer
817/// type. 'Array' represents the lvalue of the array being decayed
818/// to a pointer, and the returned SVal represents the decayed
819/// version of that lvalue (i.e., a pointer to the first element of
820/// the array). This is called by GRExprEngine when evaluating casts
821/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000822SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000823 if (!isa<loc::MemRegionVal>(Array))
824 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenekabb042f2008-12-13 19:24:37 +0000826 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
827 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000829 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000830 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000832 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000833 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000834 ArrayType *AT = cast<ArrayType>(T);
835 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek75185b52009-07-16 00:00:11 +0000837 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000838 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
839 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000840}
841
Ted Kremenek9af46f52009-06-16 22:36:44 +0000842//===----------------------------------------------------------------------===//
843// Pointer arithmetic.
844//===----------------------------------------------------------------------===//
845
Zhongxing Xu461147f2010-02-05 05:24:20 +0000846SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000847 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000848 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000849 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000850 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000851
Zhongxing Xua1718c72009-04-03 07:33:13 +0000852 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000853 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000854
Ted Kremenek3bccf082009-07-11 00:58:27 +0000855 switch (MR->getKind()) {
856 case MemRegion::SymbolicRegionKind: {
857 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000858 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000859 QualType T = Sym->getType(getContext());
860 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000862 if (const PointerType *PT = T->getAs<PointerType>())
863 EleTy = PT->getPointeeType();
864 else
John McCall183700f2009-09-21 23:43:11 +0000865 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Ted Kremenek3bccf082009-07-11 00:58:27 +0000867 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
868 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000869 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000870 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000871 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000872 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000873 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000874 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000875 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
876 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000877 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000878 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000879
Ted Kremenek3bccf082009-07-11 00:58:27 +0000880 case MemRegion::ElementRegionKind: {
881 ER = cast<ElementRegion>(MR);
882 break;
883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Ted Kremenek3bccf082009-07-11 00:58:27 +0000885 // Not yet handled.
886 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000887 case MemRegion::StringRegionKind: {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000888
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000889 }
890 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000891 case MemRegion::CompoundLiteralRegionKind:
892 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000893 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000894 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000895 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000897 case MemRegion::FunctionTextRegionKind:
898 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000899 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000900 // Technically this can happen if people do funny things with casts.
901 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremenekde0d2632010-01-05 02:18:06 +0000903 case MemRegion::CXXThisRegionKind:
904 assert(0 &&
905 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000906 case MemRegion::GenericMemSpaceRegionKind:
907 case MemRegion::StackLocalsSpaceRegionKind:
908 case MemRegion::StackArgumentsSpaceRegionKind:
909 case MemRegion::HeapSpaceRegionKind:
910 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000911 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000912 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
913 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000914 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000915
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000916 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000917 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000918
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000919 // For now, only support:
920 // (a) concrete integer indices that can easily be resolved
921 // (b) 0 + symbolic index
922 if (Base) {
923 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
924 // FIXME: Should use SValuator here.
925 SVal NewIdx =
926 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000927 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000928 const MemRegion* NewER =
929 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
930 ER->getSuperRegion(), getContext());
931 return ValMgr.makeLoc(NewER);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000932 }
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000933 if (0 == Base->getValue()) {
934 const MemRegion* NewER =
935 MRMgr.getElementRegion(ER->getElementType(), R,
936 ER->getSuperRegion(), getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000937 return ValMgr.makeLoc(NewER);
938 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Ted Kremenek5dc27462009-03-03 02:51:43 +0000941 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000942}
943
Ted Kremenek9af46f52009-06-16 22:36:44 +0000944//===----------------------------------------------------------------------===//
945// Loading values from regions.
946//===----------------------------------------------------------------------===//
947
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000948Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000949 const MemRegion *R) {
950 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000951 return *V;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000952
Zhongxing Xu13d50172009-10-11 08:08:02 +0000953 return Optional<SVal>();
954}
955
956Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000957 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000958 if (R->isBoundable())
959 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
960 if (TR->getValueType(getContext())->isUnionType())
961 return UnknownVal();
962
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000963 if (const SVal *V = Lookup(B, R, BindingKey::Default))
964 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000965
966 return Optional<SVal>();
967}
968
969Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
970 const MemRegion *R) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000971
Ted Kremenek2cf073b2010-03-30 20:30:52 +0000972 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000973 return V;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +0000974
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000975 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000976}
977
Ted Kremeneka6275a52009-07-15 02:31:43 +0000978static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
979 RTy = Ctx.getCanonicalType(RTy);
980 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Ted Kremeneka6275a52009-07-15 02:31:43 +0000982 if (RTy == UsedTy)
983 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
985
Ted Kremenek25c54572009-07-20 22:58:02 +0000986 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000987 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000988 // represents a reinterpretation.
989 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000990 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000991 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000992
993 return PUsedTy && PRTy &&
994 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000995 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000996 }
997
998 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000999}
1000
Zhongxing Xu576bb922010-02-05 03:01:53 +00001001SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001002 assert(!isa<UnknownVal>(L) && "location unknown");
1003 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001004
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001005 // FIXME: Is this even possible? Shouldn't this be treated as a null
1006 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001007 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001008 return UndefinedVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001009
Ted Kremenek67f28532009-06-17 22:02:04 +00001010 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001011
Zhongxing Xu81491852010-02-08 08:43:02 +00001012 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR))
1013 MR = GetElementZeroRegion(MR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001015 if (isa<CodeTextRegion>(MR)) {
1016 assert(0 && "Why load from a code text region?");
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001017 return UnknownVal();
Zhongxing Xu2db08ca2010-03-01 05:29:02 +00001018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001020 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1021 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001022 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001023 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001024
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001025 // FIXME: We should eventually handle funny addressing. e.g.:
1026 //
1027 // int x = ...;
1028 // int *p = &x;
1029 // char *q = (char*) p;
1030 // char c = *q; // returns the first byte of 'x'.
1031 //
1032 // Such funny addressing will occur due to layering of regions.
1033
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001034#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001035 ASTContext &Ctx = getContext();
1036 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001037 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1038 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001039 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001040 assert(Ctx.getCanonicalType(RTy) ==
1041 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001042 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001043#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001044
Douglas Gregorfb87b892010-04-26 21:31:17 +00001045 if (RTy->isStructureOrClassType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001046 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001048 // FIXME: Handle unions.
1049 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001050 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001051
1052 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +00001053 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001054
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001055 // FIXME: handle Vector types.
1056 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001057 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001058
1059 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001060 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001061
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001062 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1063 // FIXME: Here we actually perform an implicit conversion from the loaded
1064 // value to the element type. Eventually we want to compose these values
1065 // more intelligently. For example, an 'element' can encompass multiple
1066 // bound regions (e.g., several bound bytes), or could be a subset of
1067 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001068 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001071 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1072 // FIXME: Here we actually perform an implicit conversion from the loaded
1073 // value to the ivar type. What we should model is stores to ivars
1074 // that blow past the extent of the ivar. If the address of the ivar is
1075 // reinterpretted, it is possible we stored a different value that could
1076 // fit within the ivar. Either we need to cast these when storing them
1077 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001078 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001081 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1082 // FIXME: Here we actually perform an implicit conversion from the loaded
1083 // value to the variable type. What we should model is stores to variables
1084 // that blow past the extent of the variable. If the address of the
1085 // variable is reinterpretted, it is possible we stored a different value
1086 // that could fit within the variable. Either we need to cast these when
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001087 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001088 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001089 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001090
Zhongxing Xu576bb922010-02-05 03:01:53 +00001091 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001092 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001093
1094 // Check if the region has a binding.
1095 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001096 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001097
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001098 // The location does not have a bound value. This means that it has
1099 // the value it had upon its creation and/or entry to the analyzed
1100 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001101 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001102 // All stack variables are considered to have undefined values
1103 // upon creation. All heap allocated blocks are considered to
1104 // have undefined values as well unless they are explicitly bound
1105 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001106 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001107 }
1108
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001109 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001110 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001111}
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001113std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001114RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001115 if (Optional<SVal> OV = getDirectBinding(B, R))
1116 if (const nonloc::LazyCompoundVal *V =
1117 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001118 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001120 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001121 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001122 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001124 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001125 return std::make_pair(X.first,
1126 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001127 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001128 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001129 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001130 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001132 if (X.second)
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001133 return std::make_pair(X.first,
1134 MRMgr.getFieldRegionWithSuper(FR, X.second));
1135 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001136 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xudcbcbdc2010-02-10 02:02:10 +00001137 // possible for a valid lazy binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001138 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001139}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001140
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001141SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001142 const ElementRegion* R) {
1143 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001144 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001145 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001146 return *V;
1147
Ted Kremenek921109a2009-07-01 23:19:52 +00001148 const MemRegion* superR = R->getSuperRegion();
1149
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001150 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001151 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001152 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001153 // an integer, e.g., *((unsigned int*)"hello")
1154 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001155 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001156 if (T != Ctx.getCanonicalType(R->getElementType()))
1157 return UnknownVal();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001158
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001159 const StringLiteral *Str = StrR->getStringLiteral();
1160 SVal Idx = R->getIndex();
1161 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1162 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001163 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001164 if (i > byteLength) {
1165 // Buffer overflow checking in GRExprEngine should handle this case,
1166 // but we shouldn't rely on it to not overflow here if that checking
1167 // is disabled.
1168 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001169 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001170 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001171 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001172 }
1173 }
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001175 // Check if the immediate super region has a direct binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001176 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001177 if (SymbolRef parentSym = V->getAsSymbol())
1178 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001179
1180 if (V->isUnknownOrUndef())
1181 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001182
1183 // Handle LazyCompoundVals for the immediate super region. Other cases
1184 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001185 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001186 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001188 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001189 return RetrieveElement(LCV->getStore(), R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001190 }
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremeneka6275a52009-07-15 02:31:43 +00001192 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001193 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001194 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001195
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001196 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001197}
1198
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001199SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001200 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001201
1202 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001203 RegionBindings B = GetRegionBindings(store);
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001204 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001205 return *V;
1206
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001207 QualType Ty = R->getValueType(getContext());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001208 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001209}
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001211SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001212 const TypedRegion *R,
1213 QualType Ty,
1214 const MemRegion *superR) {
1215
Mike Stump1eb44332009-09-09 15:08:12 +00001216 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001217 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001219 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001221 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001222 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001223 if (SymbolRef parentSym = D->getAsSymbol())
1224 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001226 if (D->isZeroConstant())
1227 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001229 if (D->isUnknown())
1230 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001232 assert(0 && "Unknown default value");
1233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001235 // If our super region is a field or element itself, walk up the region
1236 // hierarchy to see if there is a default value installed in an ancestor.
1237 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1238 superR = cast<SubRegion>(superR)->getSuperRegion();
1239 continue;
1240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001242 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001245 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001246 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001247 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001248 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Ted Kremenek8ec4aac2010-02-09 19:11:53 +00001250 if (lazyBindingRegion) {
1251 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1252 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001253 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001254 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001255 }
1256
Ted Kremenekde0d2632010-01-05 02:18:06 +00001257 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001258 if (isa<ElementRegion>(R)) {
1259 // Currently we don't reason specially about Clang-style vectors. Check
1260 // if superR is a vector and if so return Unknown.
1261 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1262 if (typedSuperR->getValueType(getContext())->isVectorType())
1263 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001264 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001265 }
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001267 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001270 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001271 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001272}
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Zhongxing Xu576bb922010-02-05 03:01:53 +00001274SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001275
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001277 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001278
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001279 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001280 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001282 const MemRegion *superR = R->getSuperRegion();
1283
Ted Kremenekab22ee92009-10-20 01:20:57 +00001284 // Check if the super region has a default binding.
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001285 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001286 if (SymbolRef parentSym = V->getAsSymbol())
1287 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001289 // Other cases: give up.
1290 return UnknownVal();
1291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Zhongxing Xu576bb922010-02-05 03:01:53 +00001293 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001294}
1295
Zhongxing Xu576bb922010-02-05 03:01:53 +00001296SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Ted Kremenek9031dd72009-07-21 00:12:07 +00001298 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001299 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Ted Kremenek2cf073b2010-03-30 20:30:52 +00001301 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001302 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Ted Kremenek9031dd72009-07-21 00:12:07 +00001304 // Lazily derive a value for the VarRegion.
1305 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001306 QualType T = VD->getType();
1307 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001308
1309 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek4dc15662010-02-06 03:57:59 +00001310 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu14d23282010-03-01 06:56:52 +00001311 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Ted Kremenek4dc15662010-02-06 03:57:59 +00001313 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenek4552ff02010-03-30 20:31:04 +00001314 if (VD->isFileVarDecl()) {
1315 // Is 'VD' declared constant? If so, retrieve the constant value.
1316 QualType CT = Ctx.getCanonicalType(T);
1317 if (CT.isConstQualified()) {
1318 const Expr *Init = VD->getInit();
1319 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1320 if (Init)
1321 if (const IntegerLiteral *IL =
1322 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1323 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1324 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1325 IL->getType());
1326 }
1327 }
1328
Zhongxing Xu14d23282010-03-01 06:56:52 +00001329 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek4552ff02010-03-30 20:31:04 +00001330 }
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Ted Kremenek4dc15662010-02-06 03:57:59 +00001332 if (T->isIntegerType())
1333 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001334 if (T->isPointerType())
1335 return ValMgr.makeNull();
1336
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001337 return UnknownVal();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001338 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001339
Ted Kremenek9031dd72009-07-21 00:12:07 +00001340 return UndefinedVal();
1341}
1342
Zhongxing Xu576bb922010-02-05 03:01:53 +00001343SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Ted Kremenek25c54572009-07-20 22:58:02 +00001345 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001346
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001347 // All other values are symbolic.
Zhongxing Xu14d23282010-03-01 06:56:52 +00001348 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001349}
1350
Zhongxing Xu576bb922010-02-05 03:01:53 +00001351SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001352 QualType T = R->getValueType(getContext());
Douglas Gregorfb87b892010-04-26 21:31:17 +00001353 assert(T->isStructureOrClassType());
Zhongxing Xu576bb922010-02-05 03:01:53 +00001354 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001355}
1356
Zhongxing Xu576bb922010-02-05 03:01:53 +00001357SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001358 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001359 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001360}
1361
Ted Kremenek9af46f52009-06-16 22:36:44 +00001362//===----------------------------------------------------------------------===//
1363// Binding values to regions.
1364//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001365
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001366Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001367 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001368 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001369 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Ted Kremenek0964a062009-01-21 06:57:53 +00001371 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001372}
1373
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001374Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001375 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001376 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001377
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001379 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek9af46f52009-06-16 22:36:44 +00001381 // Check if the region is a struct region.
1382 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Douglas Gregorfb87b892010-04-26 21:31:17 +00001383 if (TR->getValueType(getContext())->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001384 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001386 // Special case: the current region represents a cast and it and the super
1387 // region both have pointer types or intptr_t types. If so, perform the
1388 // bind to the super region.
1389 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001390 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001391 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1392 if (ER->getIndex().isZeroConstant()) {
1393 if (const TypedRegion *superR =
1394 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1395 ASTContext &Ctx = getContext();
1396 QualType superTy = superR->getValueType(Ctx);
1397 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001398
1399 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001400 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001401 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001402 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001403 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001404 // For now, just invalidate the fields of the struct/union/class.
1405 // FIXME: Precisely handle the fields of the record.
1406 if (superTy->isRecordType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001407 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001408 }
1409 }
1410 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001411 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1412 // Binding directly to a symbolic region should be treated as binding
1413 // to element 0.
1414 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001415
Ted Kremenek852274d2009-12-16 03:18:58 +00001416 // FIXME: Is this the right way to handle symbols that are references?
1417 if (const PointerType *PT = T->getAs<PointerType>())
1418 T = PT->getPointeeType();
1419 else
1420 T = T->getAs<ReferenceType>()->getPointeeType();
1421
Ted Kremenek0954cde2009-09-24 04:11:44 +00001422 R = GetElementZeroRegion(SR, T);
1423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001425 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001426 RegionBindings B = GetRegionBindings(store);
1427 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001428}
1429
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001430Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001431 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001432
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001433 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001434
Ted Kremenek0964a062009-01-21 06:57:53 +00001435 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001436 return BindArray(store, VR, InitVal);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001437 if (T->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001438 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001439
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001440 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001441}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001442
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001443// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001444Store RegionStoreManager::BindCompoundLiteral(Store store,
1445 const CompoundLiteralExpr *CL,
1446 const LocationContext *LC,
1447 SVal V) {
1448 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001449 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001450}
1451
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001452Store RegionStoreManager::setImplicitDefaultValue(Store store,
1453 const MemRegion *R,
1454 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001455 RegionBindings B = GetRegionBindings(store);
1456 SVal V;
1457
1458 if (Loc::IsLocType(T))
1459 V = ValMgr.makeNull();
1460 else if (T->isIntegerType())
1461 V = ValMgr.makeZeroVal(T);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001462 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001463 // Set the default value to a zero constant when it is a structure
1464 // or array. The type doesn't really matter.
1465 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1466 }
1467 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001468 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001469 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001470
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001471 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001472}
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001473
1474Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001475 SVal Init) {
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001476
Ted Kremenekfee90812010-01-26 23:51:00 +00001477 ASTContext &Ctx = getContext();
1478 const ArrayType *AT =
1479 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001480 QualType ElementTy = AT->getElementType();
Ted Kremenekfee90812010-01-26 23:51:00 +00001481 Optional<uint64_t> Size;
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001482
Ted Kremenekfee90812010-01-26 23:51:00 +00001483 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1484 Size = CAT->getSize().getZExtValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001485
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001486 // Check if the init expr is a StringLiteral.
1487 if (isa<loc::MemRegionVal>(Init)) {
1488 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1489 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1490 const char* str = S->getStrData();
1491 unsigned len = S->getByteLength();
1492 unsigned j = 0;
1493
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001494 // Copy bytes from the string literal into the target array. Trailing bytes
1495 // in the array that are not covered by the string literal are initialized
1496 // to zero.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001497
Ted Kremenekfee90812010-01-26 23:51:00 +00001498 // We assume that string constants are bound to
1499 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001500 uint64_t size = Size.getValue();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001501
Ted Kremenek46537392009-07-16 01:33:37 +00001502 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001503 if (j >= len)
1504 break;
1505
Ted Kremenek46537392009-07-16 01:33:37 +00001506 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001507 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1508 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001509
Zhongxing Xud91ee272009-06-23 09:02:15 +00001510 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001511 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001512 }
1513
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001514 return store;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001515 }
1516
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001517 // Handle lazy compound values.
1518 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001519 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001520
1521 // Remaining case: explicit compound values.
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001522
Ted Kremenek027e2662009-11-19 20:20:24 +00001523 if (Init.isUnknown())
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001524 return setImplicitDefaultValue(store, R, ElementTy);
1525
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001526 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001527 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001528 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Ted Kremenekfee90812010-01-26 23:51:00 +00001530 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001531 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001532 if (VI == VE)
1533 break;
1534
Ted Kremenek46537392009-07-16 01:33:37 +00001535 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001536 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001537
Douglas Gregorfb87b892010-04-26 21:31:17 +00001538 if (ElementTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001539 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001540 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001541 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001542 }
1543
Ted Kremenek027e2662009-11-19 20:20:24 +00001544 // If the init list is shorter than the array length, set the
1545 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001546 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001547 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001548
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001549 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001550}
1551
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001552Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1553 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Ted Kremenek67f28532009-06-17 22:02:04 +00001555 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001556 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001558 QualType T = R->getValueType(getContext());
Douglas Gregorfb87b892010-04-26 21:31:17 +00001559 assert(T->isStructureOrClassType());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001560
Ted Kremenek6217b802009-07-29 21:53:49 +00001561 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001562 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001563
1564 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001565 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001566
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001567 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001568 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001569 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Ted Kremenek67f28532009-06-17 22:02:04 +00001571 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1572 // Ignore them and kill the field values.
1573 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001574 return KillStruct(store, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001575
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001576 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001577 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001578
1579 RecordDecl::field_iterator FI, FE;
1580
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001581 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001582
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001583 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001584 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001585
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001586 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001587 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001588
Ted Kremenekcf549592009-09-22 21:19:14 +00001589 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001590 store = BindArray(store, FR, *VI);
Douglas Gregorfb87b892010-04-26 21:31:17 +00001591 else if (FTy->isStructureOrClassType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001592 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001593 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001594 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001595 }
1596
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001597 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001598 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001599 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001600 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001601 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001602 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001603
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001604 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001605}
1606
Zhongxing Xu13d50172009-10-11 08:08:02 +00001607Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1608 RegionBindings B = GetRegionBindings(store);
1609 llvm::OwningPtr<RegionStoreSubRegionMap>
1610 SubRegions(getRegionStoreSubRegionMap(store));
1611 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001612
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001613 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001614 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001615}
1616
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001617Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1618 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001619
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001620 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001621 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001622
Mike Stump1eb44332009-09-09 15:08:12 +00001623 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001624 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001625
Mike Stump1eb44332009-09-09 15:08:12 +00001626 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001627 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001629 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1630 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001631 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001632}
1633
1634//===----------------------------------------------------------------------===//
1635// "Raw" retrievals and bindings.
1636//===----------------------------------------------------------------------===//
1637
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001638BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001639 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1640 const RegionRawOffset &O = ER->getAsRawOffset();
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001641
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001642 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001643 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001644
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001645 // FIXME: There are some ElementRegions for which we cannot compute
1646 // raw offsets yet, including regions with symbolic offsets.
1647 }
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001648
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001649 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001650}
1651
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001652RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001653 return RBFactory.Add(B, K, V);
1654}
1655
1656RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001657 BindingKey::Kind k, SVal V) {
1658 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001659}
1660
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001661const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001662 return B.lookup(K);
1663}
1664
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001665const SVal *RegionStoreManager::Lookup(RegionBindings B,
1666 const MemRegion *R,
1667 BindingKey::Kind k) {
1668 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001669}
1670
1671RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1672 return RBFactory.Remove(B, K);
1673}
1674
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001675RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1676 BindingKey::Kind k){
1677 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001678}
1679
1680Store RegionStoreManager::Remove(Store store, BindingKey K) {
1681 RegionBindings B = GetRegionBindings(store);
1682 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001683}
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Ted Kremenek9af46f52009-06-16 22:36:44 +00001685//===----------------------------------------------------------------------===//
1686// State pruning.
1687//===----------------------------------------------------------------------===//
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001688
Ted Kremenek5499b842010-03-10 16:32:56 +00001689namespace {
1690class RemoveDeadBindingsWorker :
1691 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1692 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1693 SymbolReaper &SymReaper;
1694 Stmt *Loc;
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001695 const StackFrameContext *CurrentLCtx;
1696
Ted Kremenek5499b842010-03-10 16:32:56 +00001697public:
1698 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1699 RegionBindings b, SymbolReaper &symReaper,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001700 Stmt *loc, const StackFrameContext *LCtx)
Ted Kremenek5499b842010-03-10 16:32:56 +00001701 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001702 SymReaper(symReaper), Loc(loc), CurrentLCtx(LCtx) {}
Ted Kremenek5499b842010-03-10 16:32:56 +00001703
1704 // Called by ClusterAnalysis.
1705 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1706 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenek5499b842010-03-10 16:32:56 +00001707
Ted Kremenek75a2d942010-04-01 00:15:55 +00001708 void VisitBindingKey(BindingKey K);
Ted Kremenek5499b842010-03-10 16:32:56 +00001709 bool UpdatePostponed();
1710 void VisitBinding(SVal V);
1711};
1712}
1713
1714void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1715 RegionCluster &C) {
1716
1717 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
1718 if (SymReaper.isLive(Loc, VR))
1719 AddToWorkList(baseR, C);
1720
1721 return;
1722 }
1723
1724 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1725 if (SymReaper.isLive(SR->getSymbol()))
1726 AddToWorkList(SR, C);
1727 else
1728 Postponed.push_back(SR);
1729
1730 return;
1731 }
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001732
1733 // CXXThisRegion in the current or parent location context is live.
1734 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
1735 const StackArgumentsSpaceRegion *StackReg =
1736 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1737 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1738 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1739 AddToWorkList(TR, C);
1740 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001741}
1742
1743void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1744 BindingKey *I, BindingKey *E) {
Ted Kremenek75a2d942010-04-01 00:15:55 +00001745 for ( ; I != E; ++I)
1746 VisitBindingKey(*I);
Ted Kremenek5499b842010-03-10 16:32:56 +00001747}
1748
1749void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1750 // Is it a LazyCompoundVal? All referenced regions are live as well.
1751 if (const nonloc::LazyCompoundVal *LCS =
1752 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1753
1754 const MemRegion *LazyR = LCS->getRegion();
1755 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1756 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
1757 const MemRegion *baseR = RI.getKey().getRegion();
1758 if (cast<SubRegion>(baseR)->isSubRegionOf(LazyR))
1759 VisitBinding(RI.getData());
1760 }
1761 return;
1762 }
1763
1764 // If V is a region, then add it to the worklist.
1765 if (const MemRegion *R = V.getAsRegion())
1766 AddToWorkList(R);
1767
1768 // Update the set of live symbols.
1769 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1770 SI!=SE;++SI)
1771 SymReaper.markLive(*SI);
1772}
1773
Ted Kremenek75a2d942010-04-01 00:15:55 +00001774void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1775 const MemRegion *R = K.getRegion();
1776
Ted Kremenek5499b842010-03-10 16:32:56 +00001777 // Mark this region "live" by adding it to the worklist. This will cause
1778 // use to visit all regions in the cluster (if we haven't visited them
1779 // already).
Ted Kremenek75a2d942010-04-01 00:15:55 +00001780 if (AddToWorkList(R)) {
1781 // Mark the symbol for any live SymbolicRegion as "live". This means we
1782 // should continue to track that symbol.
1783 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1784 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek5499b842010-03-10 16:32:56 +00001785
Ted Kremenek75a2d942010-04-01 00:15:55 +00001786 // For BlockDataRegions, enqueue the VarRegions for variables marked
1787 // with __block (passed-by-reference).
1788 // via BlockDeclRefExprs.
1789 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1790 for (BlockDataRegion::referenced_vars_iterator
1791 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1792 RI != RE; ++RI) {
1793 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1794 AddToWorkList(*RI);
1795 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001796
Ted Kremenek75a2d942010-04-01 00:15:55 +00001797 // No possible data bindings on a BlockDataRegion.
1798 return;
Ted Kremenek5499b842010-03-10 16:32:56 +00001799 }
Ted Kremenek5499b842010-03-10 16:32:56 +00001800 }
1801
Ted Kremenek75a2d942010-04-01 00:15:55 +00001802 // Visit the data binding for K.
1803 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenek5499b842010-03-10 16:32:56 +00001804 VisitBinding(*V);
1805}
1806
1807bool RemoveDeadBindingsWorker::UpdatePostponed() {
1808 // See if any postponed SymbolicRegions are actually live now, after
1809 // having done a scan.
1810 bool changed = false;
1811
1812 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1813 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1814 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1815 if (SymReaper.isLive(SR->getSymbol())) {
1816 changed |= AddToWorkList(SR);
1817 *I = NULL;
1818 }
1819 }
1820 }
1821
1822 return changed;
1823}
1824
Zhongxing Xu95798982010-05-26 03:27:35 +00001825const GRState *RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001826 const StackFrameContext *LCtx,
Zhongxing Xu72119c42010-02-05 05:34:29 +00001827 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001828 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001829{
Zhongxing Xu95798982010-05-26 03:27:35 +00001830 RegionBindings B = GetRegionBindings(state.getStore());
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +00001831 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, Loc, LCtx);
Ted Kremenek5499b842010-03-10 16:32:56 +00001832 W.GenerateClusters();
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Ted Kremenek5499b842010-03-10 16:32:56 +00001834 // Enqueue the region roots onto the worklist.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001835 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenek5499b842010-03-10 16:32:56 +00001836 E=RegionRoots.end(); I!=E; ++I)
1837 W.AddToWorkList(*I);
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001838
Ted Kremenek5499b842010-03-10 16:32:56 +00001839 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremeneke5ea0ca2010-03-10 07:20:03 +00001840
Ted Kremenek9af46f52009-06-16 22:36:44 +00001841 // We have now scanned the store, marking reachable regions and symbols
1842 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001843 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001844 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek5499b842010-03-10 16:32:56 +00001845 const BindingKey &K = I.getKey();
1846
Ted Kremenekb7118f72010-03-10 16:38:41 +00001847 // If the cluster has been visited, we know the region has been marked.
Ted Kremenek5499b842010-03-10 16:32:56 +00001848 if (W.isVisited(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001849 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremenek5499b842010-03-10 16:32:56 +00001851 // Remove the dead entry.
1852 B = Remove(B, K);
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Ted Kremenek5499b842010-03-10 16:32:56 +00001854 // Mark all non-live symbols that this binding references as dead.
1855 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001856 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001857
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001858 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001859 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1860 for (; SI != SE; ++SI)
1861 SymReaper.maybeDead(*SI);
1862 }
Zhongxing Xu95798982010-05-26 03:27:35 +00001863 state.setStore(B.getRoot());
1864 const GRState *s = StateMgr.getPersistentState(state);
1865 // Remove the extents of dead symbolic regions.
Zhongxing Xued4214c2010-05-26 03:36:08 +00001866 llvm::ImmutableMap<const MemRegion*,SVal> Extents = s->get<RegionExtents>();
Zhongxing Xu95798982010-05-26 03:27:35 +00001867 for (llvm::ImmutableMap<const MemRegion *, SVal>::iterator I=Extents.begin(),
1868 E = Extents.end(); I != E; ++I) {
1869 if (!W.isVisited(I->first))
1870 s = s->remove<RegionExtents>(I->first);
1871 }
1872 return s;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001873}
1874
Ted Kremenek5499b842010-03-10 16:32:56 +00001875
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001876GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1877 StackFrameContext const *frame) {
1878 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001879 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001880 Store store = state->getStore();
Zhongxing Xuc5063572010-03-16 13:14:16 +00001881
1882 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1883 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1884
1885 // Copy the arg expression value to the arg variables.
1886 for (; AI != AE; ++AI, ++PI) {
1887 SVal ArgVal = state->getSVal(*AI);
1888 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1889 }
1890 } else if (const CXXConstructExpr *CE =
1891 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
1892 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
1893 AE = CE->arg_end();
1894
1895 // Copy the arg expression value to the arg variables.
1896 for (; AI != AE; ++AI, ++PI) {
1897 SVal ArgVal = state->getSVal(*AI);
1898 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1899 }
1900 } else
1901 assert(0 && "Unhandled call expression.");
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001902
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001903 return state->makeWithStore(store);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001904}
1905
Ted Kremenek9af46f52009-06-16 22:36:44 +00001906//===----------------------------------------------------------------------===//
1907// Utility methods.
1908//===----------------------------------------------------------------------===//
1909
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001910void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001911 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001912 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001913 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Ted Kremenek451ac092009-08-06 04:50:20 +00001915 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001916 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001917}