blob: 93905a29dacfb23cbaa4a2e137dac7d09cce2f96 [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//===----------------------------------------------------------------------===//
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/MemRegion.h"
18#include "clang/Analysis/AnalysisContext.h"
19#include "clang/Checker/PathSensitive/GRState.h"
20#include "clang/Checker/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu3ed04d32010-01-18 08:54:31 +000024#include "clang/AST/CharUnits.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025
26#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000027#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000028#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000029
30using namespace clang;
31
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +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;
44 uint64_t Offset;
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000045
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:
49
50 bool isDefault() const { return P.getInt() == Default; }
51 bool isDirect() const { return P.getInt() == Direct; }
52
53 const MemRegion *getRegion() const { return P.getPointer(); }
54 uint64_t getOffset() const { return Offset; }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000055
56 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 Kremeneke393f4a2010-02-03 03:06:46 +000060
61 static BindingKey Make(const MemRegion *R, Kind k);
62
63 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 }
70
71 bool operator==(const BindingKey &X) const {
72 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
73 Offset == X.Offset;
74 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000075};
76} // end anonymous namespace
77
78namespace llvm {
79 static inline
80 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 Kremenekdf165012010-02-02 22:38:47 +0000177
178 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 Kremeneke393f4a2010-02-03 03:06:46 +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 Kremenek9e17cc62009-09-29 06:35:00 +0000203
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000204 typedef llvm::DenseMap<Store, RegionStoreSubRegionMap*> SMCache;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000205 SMCache SC;
206
Zhongxing Xu17892752008-10-08 02:50:44 +0000207public:
Mike Stump1eb44332009-09-09 15:08:12 +0000208 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000209 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000210 Features(f),
Ted Kremenek8928d412010-02-04 04:14:49 +0000211 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000212
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000213 virtual ~RegionStoreManager() {
214 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
215 delete (*I).second;
216 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000217
Zhongxing Xuf5416bd2010-02-05 05:18:47 +0000218 SubRegionMap *getSubRegionMap(Store store) {
219 return getRegionStoreSubRegionMap(store);
220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Zhongxing Xu13d50172009-10-11 08:08:02 +0000222 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Zhongxing Xu13d50172009-10-11 08:08:02 +0000224 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
225 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000226 /// getDefaultBinding - Returns an SVal* representing an optional default
227 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000228 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000229
230 /// setImplicitDefaultValue - Set the default binding for the provided
231 /// MemRegion to the value implicitly defined for compound literals when
232 /// the value is not specified.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000233 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000235 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
236 /// type. 'Array' represents the lvalue of the array being decayed
237 /// to a pointer, and the returned SVal represents the decayed
238 /// version of that lvalue (i.e., a pointer to the first element of
239 /// the array). This is called by GRExprEngine when evaluating
240 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000241 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000242
Zhongxing Xu461147f2010-02-05 05:24:20 +0000243 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000244
Mike Stump1eb44332009-09-09 15:08:12 +0000245 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000246 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000247 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000248
Ted Kremenek67f28532009-06-17 22:02:04 +0000249 //===-------------------------------------------------------------------===//
250 // Binding values to regions.
251 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000252
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000253 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
254 unsigned Count, InvalidatedSymbols *IS) {
255 return RegionStoreManager::InvalidateRegions(store, &R, &R+1, E, Count, IS);
Ted Kremenek81a95832009-12-03 03:27:11 +0000256 }
257
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000258 Store InvalidateRegions(Store store,
259 const MemRegion * const *Begin,
260 const MemRegion * const *End,
261 const Expr *E, unsigned Count,
262 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000264public: // Made public for helper classes.
265
Zhongxing Xu13d50172009-10-11 08:08:02 +0000266 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000267 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000269 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
270
271 RegionBindings Add(RegionBindings B, const MemRegion *R,
272 BindingKey::Kind k, SVal V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000273
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000274 const SVal *Lookup(RegionBindings B, BindingKey K);
275 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000276
277 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000278 RegionBindings Remove(RegionBindings B, const MemRegion *R,
279 BindingKey::Kind k);
280
281 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
282 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
283 }
284
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000285 Store Remove(Store store, BindingKey K);
286
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000287public: // Part of public interface to class.
288
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000289 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek67f28532009-06-17 22:02:04 +0000290
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000291 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
292 const LocationContext *LC, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000294 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000295
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000296 Store BindDeclWithNoInit(Store store, const VarRegion *) {
297 return store;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000298 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000301 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000303 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
305 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000306 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000307
Ted Kremenek67f28532009-06-17 22:02:04 +0000308 Store Remove(Store store, Loc LV);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000309
Ted Kremenek67f28532009-06-17 22:02:04 +0000310
311 //===------------------------------------------------------------------===//
312 // Loading values from regions.
313 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek67f28532009-06-17 22:02:04 +0000315 /// The high level logic for this method is this:
316 /// Retrieve (L)
317 /// if L has binding
318 /// return L's binding
319 /// else if L is in killset
320 /// return unknown
321 /// else
322 /// if L is on stack or heap
323 /// return undefined
324 /// else
325 /// return symbolic
Zhongxing Xu576bb922010-02-05 03:01:53 +0000326 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000327
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000328 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000329
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000330 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Zhongxing Xu576bb922010-02-05 03:01:53 +0000332 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Zhongxing Xu576bb922010-02-05 03:01:53 +0000334 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Zhongxing Xu576bb922010-02-05 03:01:53 +0000336 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000338 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000339 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000342 /// struct copy:
343 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000344 /// x = y;
345 /// y's value is retrieved by this method.
Zhongxing Xu576bb922010-02-05 03:01:53 +0000346 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Zhongxing Xu576bb922010-02-05 03:01:53 +0000348 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000350 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000351 std::pair<Store, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000352 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000354 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
355 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000356
Ted Kremenek0954cde2009-09-24 04:11:44 +0000357 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
358 QualType T);
359
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 //===------------------------------------------------------------------===//
361 // State pruning.
362 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek67f28532009-06-17 22:02:04 +0000364 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
365 /// It returns a new Store with these values removed.
Zhongxing Xu72119c42010-02-05 05:34:29 +0000366 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000367 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
368
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000369 const GRState *EnterStackFrame(const GRState *state,
370 const StackFrameContext *frame);
371
Ted Kremenek67f28532009-06-17 22:02:04 +0000372 //===------------------------------------------------------------------===//
373 // Region "extents".
374 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000376 const GRState *setExtent(const GRState *state,const MemRegion* R,SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000377 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000378 const MemRegion* R, QualType EleTy);
Ted Kremenek67f28532009-06-17 22:02:04 +0000379
380 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000381 // Utility methods.
382 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek451ac092009-08-06 04:50:20 +0000384 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000385 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000386 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000387
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000388 void print(Store store, llvm::raw_ostream& Out, const char* nl,
389 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000390
391 void iterBindings(Store store, BindingsHandler& f) {
392 // FIXME: Implement.
393 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000394
Ted Kremenek67f28532009-06-17 22:02:04 +0000395 // FIXME: Remove.
396 BasicValueFactory& getBasicVals() {
397 return StateMgr.getBasicVals();
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek67f28532009-06-17 22:02:04 +0000400 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000401 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000402};
403
404} // end anonymous namespace
405
Ted Kremenek9af46f52009-06-16 22:36:44 +0000406//===----------------------------------------------------------------------===//
407// RegionStore creation.
408//===----------------------------------------------------------------------===//
409
410StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
411 RegionStoreFeatures F = maximal_features_tag();
412 return new RegionStoreManager(StMgr, F);
413}
414
415StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
416 RegionStoreFeatures F = minimal_features_tag();
417 F.enableFields(true);
418 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000419}
420
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000421void
422RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000423 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000424 const MemRegion *superR = R->getSuperRegion();
425 if (add(superR, R))
426 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000427 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428}
429
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000430RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000431RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
432 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000433 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000435 llvm::SmallVector<const SubRegion*, 10> WL;
436
Ted Kremenek451ac092009-08-06 04:50:20 +0000437 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000438 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000439 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Mike Stump1eb44332009-09-09 15:08:12 +0000441 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000442 // don't have direct bindings but are super regions of those that do.
443 while (!WL.empty()) {
444 const SubRegion *R = WL.back();
445 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000446 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447 }
448
Ted Kremenek14453bf2009-03-03 19:02:42 +0000449 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000450}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000451
Ted Kremenek9af46f52009-06-16 22:36:44 +0000452//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000453// Binding invalidation.
454//===----------------------------------------------------------------------===//
455
Zhongxing Xu13d50172009-10-11 08:08:02 +0000456void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
457 const MemRegion *R,
458 RegionStoreSubRegionMap &M) {
Ted Kremenekdf165012010-02-02 22:38:47 +0000459
460 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
461 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
462 I != E; ++I)
463 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000464
465 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000466}
467
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000468namespace {
469class InvalidateRegionsWorker {
Ted Kremenek5b290652010-02-03 04:16:00 +0000470 typedef BumpVector<BindingKey> RegionCluster;
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000471 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenek5b290652010-02-03 04:16:00 +0000472 typedef llvm::SmallVector<std::pair<const MemRegion *,RegionCluster*>, 10>
473 WorkList;
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000475 BumpVectorContext BVC;
476 ClusterMap ClusterM;
477 WorkList WL;
478public:
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000479 Store InvalidateRegions(RegionStoreManager &RM, Store store,
480 const MemRegion * const *I,const MemRegion * const *E,
481 const Expr *Ex, unsigned Count,
482 StoreManager::InvalidatedSymbols *IS,
483 ASTContext &Ctx, ValueManager &ValMgr);
Ted Kremenek87806792009-09-27 20:45:21 +0000484
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000485private:
Ted Kremenek5b290652010-02-03 04:16:00 +0000486 void AddToWorkList(BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000487 void AddToWorkList(const MemRegion *R);
Ted Kremenek5b290652010-02-03 04:16:00 +0000488 void AddToCluster(BindingKey K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000489 RegionCluster **getCluster(const MemRegion *R);
490};
491}
Zhongxing Xu13d50172009-10-11 08:08:02 +0000492
Ted Kremenek5b290652010-02-03 04:16:00 +0000493void InvalidateRegionsWorker::AddToCluster(BindingKey K) {
494 const MemRegion *R = K.getRegion();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000495 const MemRegion *baseR = R->getBaseRegion();
496 RegionCluster **CPtr = getCluster(baseR);
Ted Kremenek5b290652010-02-03 04:16:00 +0000497 assert(*CPtr);
498 (*CPtr)->push_back(K, BVC);
499}
500
501void InvalidateRegionsWorker::AddToWorkList(BindingKey K) {
502 AddToWorkList(K.getRegion());
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000503}
504
505void InvalidateRegionsWorker::AddToWorkList(const MemRegion *R) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000506 const MemRegion *baseR = R->getBaseRegion();
507 RegionCluster **CPtr = getCluster(baseR);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000508 if (RegionCluster *C = *CPtr) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000509 WL.push_back(std::make_pair(baseR, C));
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000510 *CPtr = NULL;
511 }
512}
513
514InvalidateRegionsWorker::RegionCluster **
515InvalidateRegionsWorker::getCluster(const MemRegion *R) {
516 RegionCluster *&CRef = ClusterM[R];
517 if (!CRef) {
518 void *Mem = BVC.getAllocator().Allocate<RegionCluster>();
519 CRef = new (Mem) RegionCluster(BVC, 10);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000520 }
521 return &CRef;
522}
523
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000524Store InvalidateRegionsWorker::InvalidateRegions(RegionStoreManager &RM,
525 Store store,
526 const MemRegion * const *I,
527 const MemRegion * const *E,
528 const Expr *Ex, unsigned Count,
529 StoreManager::InvalidatedSymbols *IS,
530 ASTContext &Ctx,
531 ValueManager &ValMgr) {
532 RegionBindings B = RegionStoreManager::GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000533
534 // Scan the entire store and make the region clusters.
535 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000536 AddToCluster(RI.getKey());
537 if (const MemRegion *R = RI.getData().getAsRegion()) {
538 // Generate a cluster, but don't add the region to the cluster
539 // if there aren't any bindings.
540 getCluster(R->getBaseRegion());
541 }
Ted Kremenek81a95832009-12-03 03:27:11 +0000542 }
Ted Kremenek87806792009-09-27 20:45:21 +0000543
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000544 // Add the cluster for I .. E to a worklist.
545 for ( ; I != E; ++I)
546 AddToWorkList(*I);
547
548 while (!WL.empty()) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000549 const MemRegion *baseR;
550 RegionCluster *C;
551 llvm::tie(baseR, C) = WL.back();
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000552 WL.pop_back();
Ted Kremenek87806792009-09-27 20:45:21 +0000553
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000554 for (RegionCluster::iterator I = C->begin(), E = C->end(); I != E; ++I) {
Ted Kremenek5b290652010-02-03 04:16:00 +0000555 BindingKey K = *I;
Ted Kremenek473e1672009-10-16 00:30:49 +0000556
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000557 // Get the old binding. Is it a region? If so, add it to the worklist.
Ted Kremenek5b290652010-02-03 04:16:00 +0000558 if (const SVal *V = RM.Lookup(B, K)) {
559 if (const MemRegion *R = V->getAsRegion())
560 AddToWorkList(R);
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000561
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000562 // A symbol? Mark it touched by the invalidation.
563 if (IS)
564 if (SymbolRef Sym = V->getAsSymbol())
565 IS->insert(Sym);
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000566 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000567
Ted Kremenek5b290652010-02-03 04:16:00 +0000568 B = RM.Remove(B, K);
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000569 }
Ted Kremenek5b290652010-02-03 04:16:00 +0000570
571 // Now inspect the base region.
572
573 if (IS) {
574 // Symbolic region? Mark that symbol touched by the invalidation.
575 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
576 IS->insert(SR->getSymbol());
577 }
578
579 // BlockDataRegion? If so, invalidate captured variables that are passed
580 // by reference.
581 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
582 for (BlockDataRegion::referenced_vars_iterator
Ted Kremenek85248732010-02-06 00:30:00 +0000583 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
584 BI != BE; ++BI) {
585 const VarRegion *VR = *BI;
586 const VarDecl *VD = VR->getDecl();
587 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
Ted Kremenek5b290652010-02-03 04:16:00 +0000588 AddToWorkList(VR);
589 }
590 continue;
591 }
592
593 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
594 // Invalidate the region by setting its default value to
595 // conjured symbol. The type of the symbol is irrelavant.
596 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
597 Count);
598 B = RM.Add(B, baseR, BindingKey::Default, V);
599 continue;
600 }
601
602 if (!baseR->isBoundable())
603 continue;
604
605 const TypedRegion *TR = cast<TypedRegion>(baseR);
606 QualType T = TR->getValueType(Ctx);
607
608 // Invalidate the binding.
609 if (const RecordType *RT = T->getAsStructureType()) {
610 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
611 // No record definition. There is nothing we can do.
612 if (!RD) {
613 B = RM.Remove(B, baseR);
614 continue;
615 }
616
617 // Invalidate the region by setting its default value to
618 // conjured symbol. The type of the symbol is irrelavant.
619 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
620 Count);
621 B = RM.Add(B, baseR, BindingKey::Default, V);
622 continue;
623 }
624
625 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
626 // Set the default value of the array to conjured symbol.
627 DefinedOrUnknownSVal V =
628 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
629 B = RM.Add(B, baseR, BindingKey::Default, V);
630 continue;
631 }
632
633 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
634 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
635 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000636 }
637
Ted Kremenek87806792009-09-27 20:45:21 +0000638 // Create a new state with the updated bindings.
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000639 return B.getRoot();
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000640}
641
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000642Store RegionStoreManager::InvalidateRegions(Store store,
643 const MemRegion * const *I,
644 const MemRegion * const *E,
645 const Expr *Ex, unsigned Count,
646 InvalidatedSymbols *IS) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000647 InvalidateRegionsWorker W;
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000648 return W.InvalidateRegions(*this, store, I, E, Ex, Count, IS, getContext(),
649 StateMgr.getValueManager());
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000650}
651
Ted Kremenek9af46f52009-06-16 22:36:44 +0000652//===----------------------------------------------------------------------===//
653// Extents for regions.
654//===----------------------------------------------------------------------===//
655
Zhongxing Xue884ff82009-11-12 02:48:32 +0000656DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000657 const MemRegion *R,
658 QualType EleTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000660 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000661 case MemRegion::CXXThisRegionKind:
662 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000663 case MemRegion::GenericMemSpaceRegionKind:
664 case MemRegion::StackLocalsSpaceRegionKind:
665 case MemRegion::StackArgumentsSpaceRegionKind:
666 case MemRegion::HeapSpaceRegionKind:
667 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000668 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000669 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000670 return UnknownVal();
671
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000672 case MemRegion::FunctionTextRegionKind:
673 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000674 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000675 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000676 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000677
678 // Not yet handled.
679 case MemRegion::AllocaRegionKind:
680 case MemRegion::CompoundLiteralRegionKind:
681 case MemRegion::ElementRegionKind:
682 case MemRegion::FieldRegionKind:
683 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000684 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000685 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Zhongxing Xu3ed04d32010-01-18 08:54:31 +0000687 case MemRegion::SymbolicRegionKind: {
688 const SVal *Size = state->get<RegionExtents>(R);
689 if (!Size)
690 return UnknownVal();
691 const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(Size);
692 if (!CI)
693 return UnknownVal();
694
695 CharUnits RegionSize =
696 CharUnits::fromQuantity(CI->getValue().getSExtValue());
697 CharUnits EleSize = getContext().getTypeSizeInChars(EleTy);
698 assert(RegionSize % EleSize == 0);
699
700 return ValMgr.makeIntVal(RegionSize / EleSize, false);
701 }
702
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703 case MemRegion::StringRegionKind: {
704 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000705 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000706 // operations with signed indices.
707 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000710 case MemRegion::VarRegionKind: {
711 const VarRegion* VR = cast<VarRegion>(R);
712 // Get the type of the variable.
713 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000715 // FIXME: Handle variable-length arrays.
716 if (isa<VariableArrayType>(T))
717 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000719 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
720 // return the size as signed integer.
721 return ValMgr.makeIntVal(CAT->getSize(), false);
722 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000723
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000724 // Clients can use ordinary variables as if they were arrays. These
725 // essentially are arrays of size 1.
726 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000727 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000730 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000731 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000732}
733
Ted Kremenek67f28532009-06-17 22:02:04 +0000734const GRState *RegionStoreManager::setExtent(const GRState *state,
735 const MemRegion *region,
736 SVal extent) {
737 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000738}
739
740//===----------------------------------------------------------------------===//
741// Location and region casting.
742//===----------------------------------------------------------------------===//
743
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000744/// ArrayToPointer - Emulates the "decay" of an array to a pointer
745/// type. 'Array' represents the lvalue of the array being decayed
746/// to a pointer, and the returned SVal represents the decayed
747/// version of that lvalue (i.e., a pointer to the first element of
748/// the array). This is called by GRExprEngine when evaluating casts
749/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000750SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000751 if (!isa<loc::MemRegionVal>(Array))
752 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenekabb042f2008-12-13 19:24:37 +0000754 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
755 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000757 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000758 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000760 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000761 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000762 ArrayType *AT = cast<ArrayType>(T);
763 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek75185b52009-07-16 00:00:11 +0000765 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000766 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
767 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000768}
769
Ted Kremenek9af46f52009-06-16 22:36:44 +0000770//===----------------------------------------------------------------------===//
771// Pointer arithmetic.
772//===----------------------------------------------------------------------===//
773
Zhongxing Xu461147f2010-02-05 05:24:20 +0000774SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenek5c734622009-06-26 00:41:43 +0000775 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000776 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000777 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000778 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000779
Zhongxing Xua1718c72009-04-03 07:33:13 +0000780 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000781 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000782
Ted Kremenek3bccf082009-07-11 00:58:27 +0000783 switch (MR->getKind()) {
784 case MemRegion::SymbolicRegionKind: {
785 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000786 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000787 QualType T = Sym->getType(getContext());
788 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000790 if (const PointerType *PT = T->getAs<PointerType>())
791 EleTy = PT->getPointeeType();
792 else
John McCall183700f2009-09-21 23:43:11 +0000793 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Ted Kremenek3bccf082009-07-11 00:58:27 +0000795 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
796 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000797 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000798 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000799 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000800 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000801 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000802 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000803 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
804 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000805 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000806 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000807
Ted Kremenek3bccf082009-07-11 00:58:27 +0000808 case MemRegion::ElementRegionKind: {
809 ER = cast<ElementRegion>(MR);
810 break;
811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremenek3bccf082009-07-11 00:58:27 +0000813 // Not yet handled.
814 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000815 case MemRegion::StringRegionKind: {
816
817 }
818 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000819 case MemRegion::CompoundLiteralRegionKind:
820 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000821 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000822 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000823 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000825 case MemRegion::FunctionTextRegionKind:
826 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000827 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000828 // Technically this can happen if people do funny things with casts.
829 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenekde0d2632010-01-05 02:18:06 +0000831 case MemRegion::CXXThisRegionKind:
832 assert(0 &&
833 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000834 case MemRegion::GenericMemSpaceRegionKind:
835 case MemRegion::StackLocalsSpaceRegionKind:
836 case MemRegion::StackArgumentsSpaceRegionKind:
837 case MemRegion::HeapSpaceRegionKind:
838 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000839 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000840 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
841 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000842 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000843
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000844 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000845 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000846
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000847 // For now, only support:
848 // (a) concrete integer indices that can easily be resolved
849 // (b) 0 + symbolic index
850 if (Base) {
851 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
852 // FIXME: Should use SValuator here.
853 SVal NewIdx =
854 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000855 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000856 const MemRegion* NewER =
857 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
858 ER->getSuperRegion(), getContext());
859 return ValMgr.makeLoc(NewER);
860 }
861 if (0 == Base->getValue()) {
862 const MemRegion* NewER =
863 MRMgr.getElementRegion(ER->getElementType(), R,
864 ER->getSuperRegion(), getContext());
865 return ValMgr.makeLoc(NewER);
866 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000867 }
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Ted Kremenek5dc27462009-03-03 02:51:43 +0000869 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000870}
871
Ted Kremenek9af46f52009-06-16 22:36:44 +0000872//===----------------------------------------------------------------------===//
873// Loading values from regions.
874//===----------------------------------------------------------------------===//
875
Zhongxing Xu13d50172009-10-11 08:08:02 +0000876Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000877 const MemRegion *R) {
878 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
879 return *V;
880
Zhongxing Xu13d50172009-10-11 08:08:02 +0000881 return Optional<SVal>();
882}
883
884Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000885 const MemRegion *R) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000886 if (R->isBoundable())
887 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
888 if (TR->getValueType(getContext())->isUnionType())
889 return UnknownVal();
890
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000891 if (const SVal *V = Lookup(B, R, BindingKey::Default))
892 return *V;
Zhongxing Xu13d50172009-10-11 08:08:02 +0000893
894 return Optional<SVal>();
895}
896
897Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
898 const MemRegion *R) {
Ted Kremeneke393f4a2010-02-03 03:06:46 +0000899
900 if (Optional<SVal> V = getDirectBinding(B, R))
901 return V;
902
903 return getDefaultBinding(B, R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000904}
905
Ted Kremeneka6275a52009-07-15 02:31:43 +0000906static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
907 RTy = Ctx.getCanonicalType(RTy);
908 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Ted Kremeneka6275a52009-07-15 02:31:43 +0000910 if (RTy == UsedTy)
911 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000912
913
Ted Kremenek25c54572009-07-20 22:58:02 +0000914 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000915 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000916 // represents a reinterpretation.
917 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000919 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000920
921 return PUsedTy && PRTy &&
922 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000923 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000924 }
925
926 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000927}
928
Ted Kremenek0954cde2009-09-24 04:11:44 +0000929const ElementRegion *
930RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
931 ASTContext &Ctx = getContext();
932 SVal idx = ValMgr.makeZeroArrayIndex();
933 assert(!T.isNull());
934 return MRMgr.getElementRegion(T, idx, SR, Ctx);
935}
Ted Kremenek0954cde2009-09-24 04:11:44 +0000936
Zhongxing Xu576bb922010-02-05 03:01:53 +0000937SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000938 assert(!isa<UnknownVal>(L) && "location unknown");
939 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +0000940
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000941 // FIXME: Is this even possible? Shouldn't this be treated as a null
942 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000943 if (isa<loc::ConcreteInt>(L))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000944 return UndefinedVal();
Ted Kremenek28172a22009-12-15 23:23:27 +0000945
Ted Kremenek67f28532009-06-17 22:02:04 +0000946 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000947
Zhongxing Xu91844122009-05-20 09:18:48 +0000948 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000949 // Example:
950 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000951 // char* p = alloca();
952 // read(p);
953 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000954 if (isa<AllocaRegion>(MR))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000955 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek0954cde2009-09-24 04:11:44 +0000957 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
958 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremenek968f0a62009-08-03 21:41:46 +0000960 if (isa<CodeTextRegion>(MR))
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000961 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000963 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
964 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000965 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000966 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000967
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000968 // FIXME: We should eventually handle funny addressing. e.g.:
969 //
970 // int x = ...;
971 // int *p = &x;
972 // char *q = (char*) p;
973 // char c = *q; // returns the first byte of 'x'.
974 //
975 // Such funny addressing will occur due to layering of regions.
976
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000977#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000978 ASTContext &Ctx = getContext();
979 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000980 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
981 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000982 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000983 assert(Ctx.getCanonicalType(RTy) ==
984 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +0000985 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000986#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000987
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000988 if (RTy->isStructureType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000989 return RetrieveStruct(store, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000991 // FIXME: Handle unions.
992 if (RTy->isUnionType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +0000993 return UnknownVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000994
995 if (RTy->isArrayType())
Zhongxing Xu576bb922010-02-05 03:01:53 +0000996 return RetrieveArray(store, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000997
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000998 // FIXME: handle Vector types.
999 if (RTy->isVectorType())
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001000 return UnknownVal();
Zhongxing Xu99c20302009-06-28 14:16:39 +00001001
1002 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu576bb922010-02-05 03:01:53 +00001003 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001004
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001005 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
1006 // FIXME: Here we actually perform an implicit conversion from the loaded
1007 // value to the element type. Eventually we want to compose these values
1008 // more intelligently. For example, an 'element' can encompass multiple
1009 // bound regions (e.g., several bound bytes), or could be a subset of
1010 // a larger value.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001011 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001014 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
1015 // FIXME: Here we actually perform an implicit conversion from the loaded
1016 // value to the ivar type. What we should model is stores to ivars
1017 // that blow past the extent of the ivar. If the address of the ivar is
1018 // reinterpretted, it is possible we stored a different value that could
1019 // fit within the ivar. Either we need to cast these when storing them
1020 // or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001021 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001022 }
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001024 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1025 // FIXME: Here we actually perform an implicit conversion from the loaded
1026 // value to the variable type. What we should model is stores to variables
1027 // that blow past the extent of the variable. If the address of the
1028 // variable is reinterpretted, it is possible we stored a different value
1029 // that could fit within the variable. Either we need to cast these when
1030 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xu576bb922010-02-05 03:01:53 +00001031 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001032 }
Ted Kremenek25c54572009-07-20 22:58:02 +00001033
Zhongxing Xu576bb922010-02-05 03:01:53 +00001034 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001035 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001036
1037 // Check if the region has a binding.
1038 if (V)
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001039 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001040
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001041 // The location does not have a bound value. This means that it has
1042 // the value it had upon its creation and/or entry to the analyzed
1043 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001044 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001045 // All stack variables are considered to have undefined values
1046 // upon creation. All heap allocated blocks are considered to
1047 // have undefined values as well unless they are explicitly bound
1048 // to specific values.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001049 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001050 }
1051
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001052 // All other values are symbolic.
Zhongxing Xuc999ed72010-02-04 02:39:47 +00001053 return ValMgr.getRegionValueSymbolVal(R, RTy);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001054}
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001056std::pair<Store, const MemRegion *>
Ted Kremenek451ac092009-08-06 04:50:20 +00001057RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001058 if (Optional<SVal> OV = getDirectBinding(B, R))
1059 if (const nonloc::LazyCompoundVal *V =
1060 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001061 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001063 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001064 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001065 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001067 if (X.first)
1068 return std::make_pair(X.first,
1069 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001070 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001071 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001072 const std::pair<Store, const MemRegion *> &X =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001073 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001075 if (X.first)
1076 return std::make_pair(X.first,
1077 MRMgr.getFieldRegionWithSuper(FR, X.second));
1078 }
1079
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001080 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001081}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001082
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001083SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001084 const ElementRegion* R) {
1085 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001086 RegionBindings B = GetRegionBindings(store);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001087 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001088 return *V;
1089
Ted Kremenek921109a2009-07-01 23:19:52 +00001090 const MemRegion* superR = R->getSuperRegion();
1091
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001092 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001093 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001094 // FIXME: Handle loads from strings where the literal is treated as
1095 // an integer, e.g., *((unsigned int*)"hello")
1096 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001097 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001098 if (T != Ctx.getCanonicalType(R->getElementType()))
1099 return UnknownVal();
1100
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001101 const StringLiteral *Str = StrR->getStringLiteral();
1102 SVal Idx = R->getIndex();
1103 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1104 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001105 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001106 if (i > byteLength) {
1107 // Buffer overflow checking in GRExprEngine should handle this case,
1108 // but we shouldn't rely on it to not overflow here if that checking
1109 // is disabled.
1110 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001111 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001112 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001113 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001114 }
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001117 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001118 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001119 if (SymbolRef parentSym = V->getAsSymbol())
1120 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001121
1122 if (V->isUnknownOrUndef())
1123 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001124
1125 // Handle LazyCompoundVals for the immediate super region. Other cases
1126 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001127 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001128 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001130 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001131 return RetrieveElement(LCV->getStore(), R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001132 }
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremeneka6275a52009-07-15 02:31:43 +00001134 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001135 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001136 }
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001137
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001138 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001139}
1140
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001141SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001142 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001143
1144 // Check if the region has a binding.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001145 RegionBindings B = GetRegionBindings(store);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001146 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001147 return *V;
1148
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001149 QualType Ty = R->getValueType(getContext());
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001150 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001151}
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001153SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001154 const TypedRegion *R,
1155 QualType Ty,
1156 const MemRegion *superR) {
1157
Mike Stump1eb44332009-09-09 15:08:12 +00001158 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001159 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001161 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001163 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001164 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001165 if (SymbolRef parentSym = D->getAsSymbol())
1166 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001168 if (D->isZeroConstant())
1169 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001171 if (D->isUnknown())
1172 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001174 assert(0 && "Unknown default value");
1175 }
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001177 // If our super region is a field or element itself, walk up the region
1178 // hierarchy to see if there is a default value installed in an ancestor.
1179 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1180 superR = cast<SubRegion>(superR)->getSuperRegion();
1181 continue;
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001184 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001185 }
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001187 // Lazy binding?
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001188 Store lazyBindingStore = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001189 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001190 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001192 if (lazyBindingStore) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001193 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001195 if (isa<ElementRegion>(R))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001196 return RetrieveElement(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001197 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001199 return RetrieveField(lazyBindingStore,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001200 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001201 }
1202
Ted Kremenekde0d2632010-01-05 02:18:06 +00001203 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001204 if (isa<ElementRegion>(R)) {
1205 // Currently we don't reason specially about Clang-style vectors. Check
1206 // if superR is a vector and if so return Unknown.
1207 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1208 if (typedSuperR->getValueType(getContext())->isVectorType())
1209 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001210 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001213 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001216 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001217 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001218}
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Zhongxing Xu576bb922010-02-05 03:01:53 +00001220SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001221
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001222 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001223 RegionBindings B = GetRegionBindings(store);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001224
Zhongxing Xu13d50172009-10-11 08:08:02 +00001225 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001226 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001228 const MemRegion *superR = R->getSuperRegion();
1229
Ted Kremenekab22ee92009-10-20 01:20:57 +00001230 // Check if the super region has a default binding.
1231 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001232 if (SymbolRef parentSym = V->getAsSymbol())
1233 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001235 // Other cases: give up.
1236 return UnknownVal();
1237 }
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Zhongxing Xu576bb922010-02-05 03:01:53 +00001239 return RetrieveLazySymbol(R);
Ted Kremenek25c54572009-07-20 22:58:02 +00001240}
1241
Zhongxing Xu576bb922010-02-05 03:01:53 +00001242SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Ted Kremenek9031dd72009-07-21 00:12:07 +00001244 // Check if the region has a binding.
Zhongxing Xu576bb922010-02-05 03:01:53 +00001245 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Zhongxing Xu13d50172009-10-11 08:08:02 +00001247 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001248 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Ted Kremenek9031dd72009-07-21 00:12:07 +00001250 // Lazily derive a value for the VarRegion.
1251 const VarDecl *VD = R->getDecl();
Ted Kremenek4dc15662010-02-06 03:57:59 +00001252 QualType T = VD->getType();
1253 const MemSpaceRegion *MS = R->getMemorySpace();
1254
1255 if (isa<UnknownSpaceRegion>(MS) ||
1256 isa<StackArgumentsSpaceRegion>(MS))
1257 return ValMgr.getRegionValueSymbolVal(R, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek4dc15662010-02-06 03:57:59 +00001259 if (isa<GlobalsSpaceRegion>(MS)) {
1260 if (VD->isFileVarDecl())
1261 return ValMgr.getRegionValueSymbolVal(R, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenek4dc15662010-02-06 03:57:59 +00001263 if (T->isIntegerType())
1264 return ValMgr.makeIntVal(0, T);
Ted Kremenek81861ab2010-02-06 04:04:46 +00001265 if (T->isPointerType())
1266 return ValMgr.makeNull();
1267
Ted Kremenek4dc15662010-02-06 03:57:59 +00001268 return UnknownVal();
1269 }
1270
Ted Kremenek9031dd72009-07-21 00:12:07 +00001271 return UndefinedVal();
1272}
1273
Zhongxing Xu576bb922010-02-05 03:01:53 +00001274SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenek25c54572009-07-20 22:58:02 +00001276 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001277
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001278 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001279 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001280}
1281
Zhongxing Xu576bb922010-02-05 03:01:53 +00001282SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001283 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001284 assert(T->isStructureType());
1285
Zhongxing Xub7507d12009-06-11 07:27:30 +00001286 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001287 RecordDecl* RD = RT->getDecl();
1288 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001289 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001290#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001291 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1292
Ted Kremenek67f28532009-06-17 22:02:04 +00001293 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1294 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001295 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001296
Douglas Gregore267ff32008-12-11 20:41:00 +00001297 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1298 FieldEnd = Fields.rend();
1299 Field != FieldEnd; ++Field) {
1300 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001301 QualType FTy = (*Field)->getType();
Zhongxing Xu576bb922010-02-05 03:01:53 +00001302 SVal FieldValue = Retrieve(store, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001303 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1304 }
1305
Zhongxing Xud91ee272009-06-23 09:02:15 +00001306 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001307#else
Zhongxing Xu576bb922010-02-05 03:01:53 +00001308 return ValMgr.makeLazyCompoundVal(store, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001309#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001310}
1311
Zhongxing Xu576bb922010-02-05 03:01:53 +00001312SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001313#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001314 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001315 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1316
1317 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001318 uint64_t size = CAT->getSize().getZExtValue();
1319 for (uint64_t i = 0; i < size; ++i) {
1320 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001321 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001322 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001323 QualType ETy = ER->getElementType();
Zhongxing Xu576bb922010-02-05 03:01:53 +00001324 SVal ElementVal = Retrieve(store, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001325 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1326 }
1327
Zhongxing Xud91ee272009-06-23 09:02:15 +00001328 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001329#else
1330 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
Zhongxing Xu576bb922010-02-05 03:01:53 +00001331 return ValMgr.makeLazyCompoundVal(store, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001332#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001333}
1334
Ted Kremenek9af46f52009-06-16 22:36:44 +00001335//===----------------------------------------------------------------------===//
1336// Binding values to regions.
1337//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001338
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001339Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001340 if (isa<loc::MemRegionVal>(L))
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001341 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001342 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Ted Kremenek0964a062009-01-21 06:57:53 +00001344 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001345}
1346
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001347Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001348 if (isa<loc::ConcreteInt>(L))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001349 return store;
Zhongxing Xu87453d12009-06-28 10:16:11 +00001350
Ted Kremenek9af46f52009-06-16 22:36:44 +00001351 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001352 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Ted Kremenek9af46f52009-06-16 22:36:44 +00001354 // Check if the region is a struct region.
1355 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1356 if (TR->getValueType(getContext())->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001357 return BindStruct(store, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001359 // Special case: the current region represents a cast and it and the super
1360 // region both have pointer types or intptr_t types. If so, perform the
1361 // bind to the super region.
1362 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001363 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001364 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1365 if (ER->getIndex().isZeroConstant()) {
1366 if (const TypedRegion *superR =
1367 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1368 ASTContext &Ctx = getContext();
1369 QualType superTy = superR->getValueType(Ctx);
1370 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
1372 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001373 IsAnyPointerOrIntptr(erTy, Ctx)) {
Zhongxing Xu814e6b92010-02-04 04:56:43 +00001374 V = ValMgr.getSValuator().EvalCast(V, superTy, erTy);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001375 return Bind(store, loc::MemRegionVal(superR), V);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001376 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001377 // For now, just invalidate the fields of the struct/union/class.
1378 // FIXME: Precisely handle the fields of the record.
1379 if (superTy->isRecordType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001380 return InvalidateRegion(store, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001381 }
1382 }
1383 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001384 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1385 // Binding directly to a symbolic region should be treated as binding
1386 // to element 0.
1387 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek852274d2009-12-16 03:18:58 +00001388
1389 // FIXME: Is this the right way to handle symbols that are references?
1390 if (const PointerType *PT = T->getAs<PointerType>())
1391 T = PT->getPointeeType();
1392 else
1393 T = T->getAs<ReferenceType>()->getPointeeType();
1394
Ted Kremenek0954cde2009-09-24 04:11:44 +00001395 R = GetElementZeroRegion(SR, T);
1396 }
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001398 // Perform the binding.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001399 RegionBindings B = GetRegionBindings(store);
1400 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001401}
1402
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001403Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
1404 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001405
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001406 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001407
Ted Kremenek0964a062009-01-21 06:57:53 +00001408 if (T->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001409 return BindArray(store, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001410 if (T->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001411 return BindStruct(store, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001412
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001413 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001414}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001415
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001416// FIXME: this method should be merged into Bind().
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001417Store RegionStoreManager::BindCompoundLiteral(Store store,
1418 const CompoundLiteralExpr *CL,
1419 const LocationContext *LC,
1420 SVal V) {
1421 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek67d12872009-12-07 22:05:27 +00001422 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001423}
1424
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001425Store RegionStoreManager::setImplicitDefaultValue(Store store,
1426 const MemRegion *R,
1427 QualType T) {
Ted Kremenek027e2662009-11-19 20:20:24 +00001428 RegionBindings B = GetRegionBindings(store);
1429 SVal V;
1430
1431 if (Loc::IsLocType(T))
1432 V = ValMgr.makeNull();
1433 else if (T->isIntegerType())
1434 V = ValMgr.makeZeroVal(T);
1435 else if (T->isStructureType() || T->isArrayType()) {
1436 // Set the default value to a zero constant when it is a structure
1437 // or array. The type doesn't really matter.
1438 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1439 }
1440 else {
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001441 return store;
Ted Kremenek027e2662009-11-19 20:20:24 +00001442 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001443
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001444 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek027e2662009-11-19 20:20:24 +00001445}
1446
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001447Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
1448 SVal Init) {
Ted Kremenekfee90812010-01-26 23:51:00 +00001449
1450 ASTContext &Ctx = getContext();
1451 const ArrayType *AT =
1452 cast<ArrayType>(Ctx.getCanonicalType(R->getValueType(Ctx)));
1453 QualType ElementTy = AT->getElementType();
1454 Optional<uint64_t> Size;
1455
1456 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1457 Size = CAT->getSize().getZExtValue();
1458
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001459 // Check if the init expr is a StringLiteral.
1460 if (isa<loc::MemRegionVal>(Init)) {
1461 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1462 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1463 const char* str = S->getStrData();
1464 unsigned len = S->getByteLength();
1465 unsigned j = 0;
1466
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001467 // Copy bytes from the string literal into the target array. Trailing bytes
1468 // in the array that are not covered by the string literal are initialized
1469 // to zero.
Ted Kremenekfee90812010-01-26 23:51:00 +00001470
1471 // We assume that string constants are bound to
1472 // constant arrays.
Ted Kremenek39c2ea12010-01-27 16:31:37 +00001473 uint64_t size = Size.getValue();
Ted Kremenekfee90812010-01-26 23:51:00 +00001474
Ted Kremenek46537392009-07-16 01:33:37 +00001475 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001476 if (j >= len)
1477 break;
1478
Ted Kremenek46537392009-07-16 01:33:37 +00001479 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001480 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1481 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001482
Zhongxing Xud91ee272009-06-23 09:02:15 +00001483 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001484 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001485 }
1486
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001487 return store;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001488 }
1489
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001490 // Handle lazy compound values.
1491 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001492 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001493
1494 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001495
1496 if (Init.isUnknown())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001497 return setImplicitDefaultValue(store, R, ElementTy);
Ted Kremenek027e2662009-11-19 20:20:24 +00001498
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001499 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001500 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001501 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Ted Kremenekfee90812010-01-26 23:51:00 +00001503 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001504 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001505 if (VI == VE)
1506 break;
1507
Ted Kremenek46537392009-07-16 01:33:37 +00001508 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001509 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001510
Ted Kremenekfee90812010-01-26 23:51:00 +00001511 if (ElementTy->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001512 store = BindStruct(store, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001513 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001514 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001515 }
1516
Ted Kremenek027e2662009-11-19 20:20:24 +00001517 // If the init list is shorter than the array length, set the
1518 // array default value.
Ted Kremenekfee90812010-01-26 23:51:00 +00001519 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001520 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001521
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001522 return store;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001523}
1524
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001525Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1526 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Ted Kremenek67f28532009-06-17 22:02:04 +00001528 if (!Features.supportsFields())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001529 return store;
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001531 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001532 assert(T->isStructureType());
1533
Ted Kremenek6217b802009-07-29 21:53:49 +00001534 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001535 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001536
1537 if (!RD->isDefinition())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001538 return store;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001539
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001540 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001541 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001542 return CopyLazyBindings(*LCV, store, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek67f28532009-06-17 22:02:04 +00001544 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1545 // Ignore them and kill the field values.
1546 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001547 return KillStruct(store, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001548
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001549 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001550 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001551
1552 RecordDecl::field_iterator FI, FE;
1553
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001554 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001555
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001556 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001557 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001558
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001559 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001560 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001561
Ted Kremenekcf549592009-09-22 21:19:14 +00001562 if (FTy->isArrayType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001563 store = BindArray(store, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001564 else if (FTy->isStructureType())
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001565 store = BindStruct(store, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001566 else
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001567 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001568 }
1569
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001570 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001571 if (FI != FE) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001572 RegionBindings B = GetRegionBindings(store);
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001573 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001574 store = B.getRoot();
Zhongxing Xu13d50172009-10-11 08:08:02 +00001575 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001576
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001577 return store;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001578}
1579
Zhongxing Xu13d50172009-10-11 08:08:02 +00001580Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1581 RegionBindings B = GetRegionBindings(store);
1582 llvm::OwningPtr<RegionStoreSubRegionMap>
1583 SubRegions(getRegionStoreSubRegionMap(store));
1584 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001585
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001586 // Set the default value of the struct region to "unknown".
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001587 return Add(B, R, BindingKey::Default, UnknownVal()).getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001588}
1589
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001590Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1591 Store store, const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001592
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001593 // Nuke the old bindings stemming from R.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001594 RegionBindings B = GetRegionBindings(store);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001595
Mike Stump1eb44332009-09-09 15:08:12 +00001596 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001597 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001598
Mike Stump1eb44332009-09-09 15:08:12 +00001599 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001600 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001602 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1603 // results in a zero-copy algorithm.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001604 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001605}
1606
1607//===----------------------------------------------------------------------===//
1608// "Raw" retrievals and bindings.
1609//===----------------------------------------------------------------------===//
1610
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001611BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001612 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1613 const RegionRawOffset &O = ER->getAsRawOffset();
1614
1615 if (O.getRegion())
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001616 return BindingKey(O.getRegion(), O.getByteOffset(), k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001617
1618 // FIXME: There are some ElementRegions for which we cannot compute
1619 // raw offsets yet, including regions with symbolic offsets.
1620 }
1621
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001622 return BindingKey(R, 0, k);
Ted Kremenekc50e6df2010-01-11 02:33:26 +00001623}
1624
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001625RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001626 return RBFactory.Add(B, K, V);
1627}
1628
1629RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001630 BindingKey::Kind k, SVal V) {
1631 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001632}
1633
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001634const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001635 return B.lookup(K);
1636}
1637
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001638const SVal *RegionStoreManager::Lookup(RegionBindings B,
1639 const MemRegion *R,
1640 BindingKey::Kind k) {
1641 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001642}
1643
1644RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1645 return RBFactory.Remove(B, K);
1646}
1647
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001648RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1649 BindingKey::Kind k){
1650 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001651}
1652
1653Store RegionStoreManager::Remove(Store store, BindingKey K) {
1654 RegionBindings B = GetRegionBindings(store);
1655 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001656}
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek9af46f52009-06-16 22:36:44 +00001658//===----------------------------------------------------------------------===//
1659// State pruning.
1660//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001661
Zhongxing Xu72119c42010-02-05 05:34:29 +00001662Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
1663 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001664 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001665{
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001666 typedef std::pair<Store, const MemRegion *> RBDNode;
Ted Kremenek781115c2009-10-17 17:45:11 +00001667
Ted Kremenek451ac092009-08-06 04:50:20 +00001668 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Ted Kremenek9af46f52009-06-16 22:36:44 +00001670 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001671 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001672 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001673
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001674 // Do a pass over the regions in the store. For VarRegions we check if
1675 // the variable is still live and if so add it to the list of live roots.
1676 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001678
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001679 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001680 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001681 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001683 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001684
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001685 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001686 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001687 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001688 llvm::SmallVector<RBDNode, 10> Postponed;
1689
Zhongxing Xu6800b332009-10-18 04:15:47 +00001690 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001691
Ted Kremenek9af46f52009-06-16 22:36:44 +00001692 while (!IntermediateRoots.empty()) {
1693 const MemRegion* R = IntermediateRoots.back();
1694 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001695
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001696 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001697 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001698 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001699
Ted Kremenek9af46f52009-06-16 22:36:44 +00001700 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001701 if (SymReaper.isLive(Loc, VR))
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001702 WorkList.push_back(std::make_pair(store, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001703 continue;
1704 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001705
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001706 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001707 llvm::SmallVectorImpl<RBDNode> &Q =
1708 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1709
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001710 Q.push_back(std::make_pair(store, SR));
Ted Kremenek01756192009-10-29 05:14:17 +00001711
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001712 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001713 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001714
1715 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001716 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001717 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001718 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001719 }
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001721 // Enqueue the RegionRoots onto WorkList.
1722 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1723 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001724 WorkList.push_back(std::make_pair(store, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001725 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001726 RegionRoots.clear();
1727
Zhongxing Xu6800b332009-10-18 04:15:47 +00001728 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001729
Ted Kremenek01756192009-10-29 05:14:17 +00001730tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001731 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001732 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001733 WorkList.pop_back();
1734
1735 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001736 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001737 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001738 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001740 const MemRegion *R = N.second;
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001741 Store store_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001742
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001743 // Enqueue subregions.
1744 RegionStoreSubRegionMap *M;
1745
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001746 if (store == store_N)
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001747 M = SubRegions.get();
1748 else {
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001749 RegionStoreSubRegionMap *& SM = SC[store_N];
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001750 if (!SM)
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001751 SM = getRegionStoreSubRegionMap(store_N);
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001752 M = SM;
1753 }
Ted Kremenekdf165012010-02-02 22:38:47 +00001754
1755 if (const RegionStoreSubRegionMap::Set *S = M->getSubRegions(R))
1756 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
1757 I != E; ++I)
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001758 WorkList.push_back(std::make_pair(store_N, *I));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001759
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001760 // Enqueue the super region.
1761 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1762 const MemRegion *superR = SR->getSuperRegion();
1763 if (!isa<MemSpaceRegion>(superR)) {
1764 // If 'R' is a field or an element, we want to keep the bindings
1765 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001766 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001767 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1768 || isa<ObjCIvarRegion>(R));
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001769 WorkList.push_back(std::make_pair(store_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001770 }
1771 }
1772
1773 // Mark the symbol for any live SymbolicRegion as "live". This means we
1774 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001775 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001776 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001777
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001778 // For BlockDataRegions, enqueue the VarRegions for variables marked
1779 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001780 // via BlockDeclRefExprs.
1781 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1782 for (BlockDataRegion::referenced_vars_iterator
1783 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001784 RI != RE; ++RI) {
1785 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001786 WorkList.push_back(std::make_pair(store_N, *RI));
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001787 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001788 // No possible data bindings on a BlockDataRegion. Continue to the
1789 // next region in the worklist.
1790 continue;
1791 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001792
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001793 RegionBindings B_N = GetRegionBindings(store_N);
1794
1795 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001796 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001797
Zhongxing Xu13d50172009-10-11 08:08:02 +00001798 if (V) {
1799 // Check for lazy bindings.
1800 if (const nonloc::LazyCompoundVal *LCV =
1801 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001802
Zhongxing Xu13d50172009-10-11 08:08:02 +00001803 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001804 WorkList.push_back(std::make_pair(D->getStore(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001805 }
1806 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001807 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001808 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001809 SI!=SE;++SI)
1810 SymReaper.markLive(*SI);
1811
Zhongxing Xu13d50172009-10-11 08:08:02 +00001812 // If V is a region, then add it to the worklist.
1813 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001814 WorkList.push_back(std::make_pair(store_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001815 }
1816 }
1817 }
1818
Ted Kremenek01756192009-10-29 05:14:17 +00001819 // See if any postponed SymbolicRegions are actually live now, after
1820 // having done a scan.
1821 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1822 E = Postponed.end() ; I != E ; ++I) {
1823 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1824 if (SymReaper.isLive(SR->getSymbol())) {
1825 WorkList.push_back(*I);
1826 I->second = NULL;
1827 }
1828 }
1829 }
1830
1831 if (!WorkList.empty())
1832 goto tryAgain;
1833
Ted Kremenek9af46f52009-06-16 22:36:44 +00001834 // We have now scanned the store, marking reachable regions and symbols
1835 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001836 // as well as update DSymbols with the set symbols that are now dead.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001837 Store new_store = store;
Ted Kremenek451ac092009-08-06 04:50:20 +00001838 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001839 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001840 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001841 if (Visited.count(std::make_pair(store, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001842 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Ted Kremenek9af46f52009-06-16 22:36:44 +00001844 // Remove this dead region from the store.
Zhongxing Xubfcaf802010-02-05 02:26:30 +00001845 new_store = Remove(new_store, I.getKey());
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Ted Kremenek9af46f52009-06-16 22:36:44 +00001847 // Mark all non-live symbols that this region references as dead.
1848 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1849 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremeneke393f4a2010-02-03 03:06:46 +00001851 SVal X = I.getData();
Ted Kremenek093569c2009-08-02 05:00:15 +00001852 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1853 for (; SI != SE; ++SI)
1854 SymReaper.maybeDead(*SI);
1855 }
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Zhongxing Xu72119c42010-02-05 05:34:29 +00001857 return new_store;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001858}
1859
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001860GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1861 StackFrameContext const *frame) {
1862 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1863 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1864
1865 FunctionDecl::param_const_iterator PI = FD->param_begin();
1866
1867 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1868
1869 // Copy the arg expression value to the arg variables.
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001870 Store store = state->getStore();
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001871 for (; AI != AE; ++AI, ++PI) {
1872 SVal ArgVal = state->getSVal(*AI);
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001873 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001874 }
1875
Zhongxing Xub4a9c612010-02-05 05:06:13 +00001876 return state->makeWithStore(store);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001877}
1878
Ted Kremenek9af46f52009-06-16 22:36:44 +00001879//===----------------------------------------------------------------------===//
1880// Utility methods.
1881//===----------------------------------------------------------------------===//
1882
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001883void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001884 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001885 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001886 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001887
Ted Kremenek451ac092009-08-06 04:50:20 +00001888 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001889 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001890}