blob: 19945a7b3c8f1b198003b971dea2270788346d26 [file] [log] [blame]
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
Zhongxing Xu228b0d42010-01-18 08:54:31 +000017#include "clang/AST/CharUnits.h"
Zhongxing Xu0eb69032010-03-16 13:14:16 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/ExprCXX.h"
Ted Kremenek26984fb2010-04-09 20:26:58 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Basic/TargetInfo.h"
23#include "clang/Checker/PathSensitive/GRState.h"
24#include "clang/Checker/PathSensitive/GRStateTrait.h"
25#include "clang/Checker/PathSensitive/MemRegion.h"
Zhongxing Xuceca8062008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Ted Kremenek26984fb2010-04-09 20:26:58 +000027#include "llvm/ADT/ImmutableMap.h"
28#include "llvm/ADT/Optional.h"
Zhongxing Xu1359e002008-10-24 06:01:33 +000029#include "llvm/Support/raw_ostream.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000030
31using namespace clang;
Ted Kremenek26984fb2010-04-09 20:26:58 +000032using llvm::Optional;
Zhongxing Xud9959ae2008-10-08 02:50:44 +000033
Ted Kremenek8e994a22010-01-11 00:07:44 +000034//===----------------------------------------------------------------------===//
Ted Kremenek8e994a22010-01-11 00:07:44 +000035// Representation of binding keys.
36//===----------------------------------------------------------------------===//
37
38namespace {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000039class BindingKey {
Ted Kremenek8e994a22010-01-11 00:07:44 +000040public:
Ted Kremenek64efd0d2010-02-03 03:06:46 +000041 enum Kind { Direct = 0x0, Default = 0x1 };
42private:
43 llvm ::PointerIntPair<const MemRegion*, 1> P;
Ted Kremenekba2e6c62010-03-10 07:20:03 +000044 uint64_t Offset;
45
Ted Kremenek64efd0d2010-02-03 03:06:46 +000046 explicit BindingKey(const MemRegion *r, uint64_t offset, Kind k)
Jordy Rosedf28e8e2010-08-16 01:15:17 +000047 : P(r, (unsigned) k), Offset(offset) {}
Ted Kremenek64efd0d2010-02-03 03:06:46 +000048public:
Ted Kremenekba2e6c62010-03-10 07:20:03 +000049
Ted Kremenek64efd0d2010-02-03 03:06:46 +000050 bool isDirect() const { return P.getInt() == Direct; }
Ted Kremenekba2e6c62010-03-10 07:20:03 +000051
Ted Kremenek64efd0d2010-02-03 03:06:46 +000052 const MemRegion *getRegion() const { return P.getPointer(); }
53 uint64_t getOffset() const { return Offset; }
Ted Kremenekba2e6c62010-03-10 07:20:03 +000054
Ted Kremenek8e994a22010-01-11 00:07:44 +000055 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000056 ID.AddPointer(P.getOpaqueValue());
57 ID.AddInteger(Offset);
Ted Kremenek8e994a22010-01-11 00:07:44 +000058 }
Ted Kremenekba2e6c62010-03-10 07:20:03 +000059
Ted Kremenek64efd0d2010-02-03 03:06:46 +000060 static BindingKey Make(const MemRegion *R, Kind k);
Ted Kremenekba2e6c62010-03-10 07:20:03 +000061
Ted Kremenek64efd0d2010-02-03 03:06:46 +000062 bool operator<(const BindingKey &X) const {
63 if (P.getOpaqueValue() < X.P.getOpaqueValue())
64 return true;
65 if (P.getOpaqueValue() > X.P.getOpaqueValue())
66 return false;
67 return Offset < X.Offset;
68 }
Ted Kremenekba2e6c62010-03-10 07:20:03 +000069
Ted Kremenek64efd0d2010-02-03 03:06:46 +000070 bool operator==(const BindingKey &X) const {
71 return P.getOpaqueValue() == X.P.getOpaqueValue() &&
72 Offset == X.Offset;
73 }
Jordy Rosedf28e8e2010-08-16 01:15:17 +000074
Jordy Rose63a38a12010-08-16 20:53:01 +000075 bool isValid() const {
Jordy Rosedf28e8e2010-08-16 01:15:17 +000076 return getRegion() != NULL;
77 }
Ted Kremenekba2e6c62010-03-10 07:20:03 +000078};
Ted Kremenek8e994a22010-01-11 00:07:44 +000079} // end anonymous namespace
80
Zhongxing Xufaf150b2010-08-21 12:24:38 +000081BindingKey BindingKey::Make(const MemRegion *R, Kind k) {
82 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
83 const RegionRawOffset &O = ER->getAsArrayOffset();
84
85 // FIXME: There are some ElementRegions for which we cannot compute
86 // raw offsets yet, including regions with symbolic offsets. These will be
87 // ignored by the store.
88 return BindingKey(O.getRegion(), O.getByteOffset(), k);
89 }
90
91 return BindingKey(R, 0, k);
92}
93
Ted Kremenek8e994a22010-01-11 00:07:44 +000094namespace llvm {
Ted Kremenekba2e6c62010-03-10 07:20:03 +000095 static inline
Ted Kremenek8e994a22010-01-11 00:07:44 +000096 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
Ted Kremenek64efd0d2010-02-03 03:06:46 +000097 os << '(' << K.getRegion() << ',' << K.getOffset()
98 << ',' << (K.isDirect() ? "direct" : "default")
99 << ')';
Ted Kremenek8e994a22010-01-11 00:07:44 +0000100 return os;
101 }
102} // end llvm namespace
103
104//===----------------------------------------------------------------------===//
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000105// Actual Store type.
Ted Kremenek8e994a22010-01-11 00:07:44 +0000106//===----------------------------------------------------------------------===//
107
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000108typedef llvm::ImmutableMap<BindingKey, SVal> RegionBindings;
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000109
Ted Kremenekae189ec2008-12-24 01:05:03 +0000110//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000111// Fine-grained control of RegionStoreManager.
112//===----------------------------------------------------------------------===//
113
114namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000115struct minimal_features_tag {};
116struct maximal_features_tag {};
Mike Stump11289f42009-09-09 15:08:12 +0000117
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000118class RegionStoreFeatures {
Ted Kremenek4533a552009-06-16 22:36:44 +0000119 bool SupportsFields;
Ted Kremenek4533a552009-06-16 22:36:44 +0000120public:
121 RegionStoreFeatures(minimal_features_tag) :
Ted Kremenek87562e82010-08-20 06:06:41 +0000122 SupportsFields(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000123
Ted Kremenek4533a552009-06-16 22:36:44 +0000124 RegionStoreFeatures(maximal_features_tag) :
Ted Kremenek87562e82010-08-20 06:06:41 +0000125 SupportsFields(true) {}
Mike Stump11289f42009-09-09 15:08:12 +0000126
Ted Kremenek4533a552009-06-16 22:36:44 +0000127 void enableFields(bool t) { SupportsFields = t; }
Mike Stump11289f42009-09-09 15:08:12 +0000128
Ted Kremenek4533a552009-06-16 22:36:44 +0000129 bool supportsFields() const { return SupportsFields; }
Ted Kremenek4533a552009-06-16 22:36:44 +0000130};
131}
132
133//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000134// Main RegionStore logic.
135//===----------------------------------------------------------------------===//
Ted Kremenek677779a2008-12-04 02:08:27 +0000136
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000137namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000138
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000139class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenekb251eb62010-02-02 22:38:47 +0000140public:
141 typedef llvm::ImmutableSet<const MemRegion*> Set;
142 typedef llvm::DenseMap<const MemRegion*, Set> Map;
143private:
144 Set::Factory F;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000145 Map M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000146public:
Ted Kremenek844a7292009-08-05 19:09:24 +0000147 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000148 Map::iterator I = M.find(Parent);
Ted Kremenek844a7292009-08-05 19:09:24 +0000149
150 if (I == M.end()) {
Ted Kremenek68c1f012009-08-05 05:31:02 +0000151 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenek844a7292009-08-05 19:09:24 +0000152 return true;
153 }
154
155 I->second = F.Add(I->second, SubRegion);
156 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000157 }
Mike Stump11289f42009-09-09 15:08:12 +0000158
Ted Kremenekfa417142009-08-06 01:20:57 +0000159 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000160
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000161 ~RegionStoreSubRegionMap() {}
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000162
Ted Kremenekb251eb62010-02-02 22:38:47 +0000163 const Set *getSubRegions(const MemRegion *Parent) const {
164 Map::const_iterator I = M.find(Parent);
165 return I == M.end() ? NULL : &I->second;
166 }
Mike Stump11289f42009-09-09 15:08:12 +0000167
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000168 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +0000169 Map::const_iterator I = M.find(Parent);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000170
171 if (I == M.end())
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000172 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000173
Ted Kremenekb251eb62010-02-02 22:38:47 +0000174 Set S = I->second;
175 for (Set::iterator SI=S.begin(),SE=S.end(); SI != SE; ++SI) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000176 if (!V.Visit(Parent, *SI))
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000177 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000178 }
Mike Stump11289f42009-09-09 15:08:12 +0000179
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000180 return true;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182};
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000183
Zhongxing Xufee32dc2010-08-23 01:37:32 +0000184void
185RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
186 const SubRegion *R) {
187 const MemRegion *superR = R->getSuperRegion();
188 if (add(superR, R))
189 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
190 WL.push_back(sr);
191}
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000192
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000193class RegionStoreManager : public StoreManager {
Ted Kremenek4533a552009-06-16 22:36:44 +0000194 const RegionStoreFeatures Features;
Ted Kremenek2c85f172009-08-06 04:50:20 +0000195 RegionBindings::Factory RBFactory;
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000196
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000197public:
Mike Stump11289f42009-09-09 15:08:12 +0000198 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek43015262009-07-29 21:43:22 +0000199 : StoreManager(mgr),
Ted Kremenek4533a552009-06-16 22:36:44 +0000200 Features(f),
Ted Kremenek33617e02010-02-04 04:14:49 +0000201 RBFactory(mgr.getAllocator()) {}
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000202
Zhongxing Xuf6682042010-02-05 05:18:47 +0000203 SubRegionMap *getSubRegionMap(Store store) {
204 return getRegionStoreSubRegionMap(store);
205 }
Mike Stump11289f42009-09-09 15:08:12 +0000206
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000207 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump11289f42009-09-09 15:08:12 +0000208
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000209 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000210 /// getDefaultBinding - Returns an SVal* representing an optional default
211 /// binding associated with a region and its subregions.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000212 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000213
Ted Kremenek439a6d12009-11-19 20:20:24 +0000214 /// setImplicitDefaultValue - Set the default binding for the provided
215 /// MemRegion to the value implicitly defined for compound literals when
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000216 /// the value is not specified.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000217 Store setImplicitDefaultValue(Store store, const MemRegion *R, QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000218
Ted Kremenek2907ab72008-12-24 07:46:32 +0000219 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
220 /// type. 'Array' represents the lvalue of the array being decayed
221 /// to a pointer, and the returned SVal represents the decayed
222 /// version of that lvalue (i.e., a pointer to the first element of
223 /// the array). This is called by GRExprEngine when evaluating
224 /// casts from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000225 SVal ArrayToPointer(Loc Array);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000226
Zhongxing Xu0d081f32010-02-05 05:24:20 +0000227 SVal EvalBinOp(BinaryOperator::Opcode Op,Loc L, NonLoc R, QualType resultTy);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000228
Mike Stump11289f42009-09-09 15:08:12 +0000229 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000230 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000231 }
Ted Kremenek608677a2009-08-21 23:25:54 +0000232
Ted Kremenek609df302009-06-17 22:02:04 +0000233 //===-------------------------------------------------------------------===//
234 // Binding values to regions.
235 //===-------------------------------------------------------------------===//
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000236
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000237 Store InvalidateRegions(Store store,
238 const MemRegion * const *Begin,
239 const MemRegion * const *End,
240 const Expr *E, unsigned Count,
Ted Kremenekbd862712010-07-01 20:16:50 +0000241 InvalidatedSymbols *IS,
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000242 bool invalidateGlobals,
243 InvalidatedRegions *Regions);
Mike Stump11289f42009-09-09 15:08:12 +0000244
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000245public: // Made public for helper classes.
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000246
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000247 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremenekfa417142009-08-06 01:20:57 +0000248 RegionStoreSubRegionMap &M);
Mike Stump11289f42009-09-09 15:08:12 +0000249
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000250 RegionBindings Add(RegionBindings B, BindingKey K, SVal V);
251
252 RegionBindings Add(RegionBindings B, const MemRegion *R,
253 BindingKey::Kind k, SVal V);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000254
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000255 const SVal *Lookup(RegionBindings B, BindingKey K);
256 const SVal *Lookup(RegionBindings B, const MemRegion *R, BindingKey::Kind k);
Ted Kremenek8e994a22010-01-11 00:07:44 +0000257
258 RegionBindings Remove(RegionBindings B, BindingKey K);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000259 RegionBindings Remove(RegionBindings B, const MemRegion *R,
260 BindingKey::Kind k);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000261
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000262 RegionBindings Remove(RegionBindings B, const MemRegion *R) {
263 return Remove(Remove(B, R, BindingKey::Direct), R, BindingKey::Default);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000264 }
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000265
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000266public: // Part of public interface to class.
267
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000268 Store Bind(Store store, Loc LV, SVal V);
Ted Kremenek609df302009-06-17 22:02:04 +0000269
Zhongxing Xuce836562010-06-01 04:49:26 +0000270 // BindDefault is only used to initialize a region with a default value.
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000271 Store BindDefault(Store store, const MemRegion *R, SVal V) {
Zhongxing Xuce836562010-06-01 04:49:26 +0000272 RegionBindings B = GetRegionBindings(store);
273 assert(!Lookup(B, R, BindingKey::Default));
274 assert(!Lookup(B, R, BindingKey::Direct));
275 return Add(B, R, BindingKey::Default, V).getRoot();
Zhongxing Xu527ff6d2010-06-01 03:01:33 +0000276 }
277
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000278 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL,
279 const LocationContext *LC, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000280
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000281 Store BindDecl(Store store, const VarRegion *VR, SVal InitVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000282
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000283 Store BindDeclWithNoInit(Store store, const VarRegion *) {
284 return store;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000285 }
Zhongxing Xu83aff702008-10-21 05:29:26 +0000286
Ted Kremenek609df302009-06-17 22:02:04 +0000287 /// BindStruct - Bind a compound value to a structure.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000288 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000289
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000290 Store BindArray(Store store, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000291
292 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek385f71b2010-07-29 00:28:47 +0000293 Store KillStruct(Store store, const TypedRegion* R, SVal DefaultVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000294
Ted Kremenek609df302009-06-17 22:02:04 +0000295 Store Remove(Store store, Loc LV);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000296
Ted Kremenek609df302009-06-17 22:02:04 +0000297
298 //===------------------------------------------------------------------===//
299 // Loading values from regions.
300 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenek609df302009-06-17 22:02:04 +0000302 /// The high level logic for this method is this:
303 /// Retrieve (L)
304 /// if L has binding
305 /// return L's binding
306 /// else if L is in killset
307 /// return unknown
308 /// else
309 /// if L is on stack or heap
310 /// return undefined
311 /// else
312 /// return symbolic
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000313 SVal Retrieve(Store store, Loc L, QualType T = QualType());
Zhongxing Xue67ea5c2009-06-25 04:50:44 +0000314
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000315 SVal RetrieveElement(Store store, const ElementRegion *R);
Zhongxing Xu2d160732009-06-25 05:29:39 +0000316
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000317 SVal RetrieveField(Store store, const FieldRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000318
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000319 SVal RetrieveObjCIvar(Store store, const ObjCIvarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000320
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000321 SVal RetrieveVar(Store store, const VarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000322
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000323 SVal RetrieveLazySymbol(const TypedRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000324
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000325 SVal RetrieveFieldOrElementCommon(Store store, const TypedRegion *R,
Ted Kremenek040e3b92009-08-06 22:33:36 +0000326 QualType Ty, const MemRegion *superR);
Mike Stump11289f42009-09-09 15:08:12 +0000327
Ted Kremenek609df302009-06-17 22:02:04 +0000328 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump11289f42009-09-09 15:08:12 +0000329 /// struct copy:
330 /// struct s x, y;
Ted Kremenek609df302009-06-17 22:02:04 +0000331 /// x = y;
332 /// y's value is retrieved by this method.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000333 SVal RetrieveStruct(Store store, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000334
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000335 SVal RetrieveArray(Store store, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenekbd862712010-07-01 20:16:50 +0000337 /// Used to lazily generate derived symbols for bindings that are defined
338 /// implicitly by default bindings in a super region.
339 Optional<SVal> RetrieveDerivedDefaultValue(RegionBindings B,
340 const MemRegion *superR,
341 const TypedRegion *R, QualType Ty);
342
Zhongxing Xufd62a332009-12-21 06:52:24 +0000343 /// Get the state and region whose binding this region R corresponds to.
Zhongxing Xubd96bf12010-02-05 02:26:30 +0000344 std::pair<Store, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +0000345 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000346
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000347 Store CopyLazyBindings(nonloc::LazyCompoundVal V, Store store,
348 const TypedRegion *R);
Ted Kremenek609df302009-06-17 22:02:04 +0000349
350 //===------------------------------------------------------------------===//
351 // State pruning.
352 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000353
Ted Kremenek609df302009-06-17 22:02:04 +0000354 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
355 /// It returns a new Store with these values removed.
Zhongxing Xuee770d42010-08-15 12:45:09 +0000356 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
357 SymbolReaper& SymReaper,
Ted Kremenek609df302009-06-17 22:02:04 +0000358 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
359
Jordy Roseddec0922010-08-03 20:44:35 +0000360 Store EnterStackFrame(const GRState *state, const StackFrameContext *frame);
Zhongxing Xudaa41762009-10-13 02:24:55 +0000361
Ted Kremenek609df302009-06-17 22:02:04 +0000362 //===------------------------------------------------------------------===//
363 // Region "extents".
364 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000365
Jordy Rose674bd552010-07-04 00:00:41 +0000366 // FIXME: This method will soon be eliminated; see the note in Store.h.
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000367 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000368 const MemRegion* R, QualType EleTy);
Ted Kremenek609df302009-06-17 22:02:04 +0000369
370 //===------------------------------------------------------------------===//
Ted Kremenek609df302009-06-17 22:02:04 +0000371 // Utility methods.
372 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000373
Ted Kremenek2c85f172009-08-06 04:50:20 +0000374 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000375 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000376 }
Zhongxing Xucebb7412008-10-24 01:38:55 +0000377
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000378 void print(Store store, llvm::raw_ostream& Out, const char* nl,
379 const char *sep);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000380
381 void iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek648ef7a2010-06-17 00:24:42 +0000382 RegionBindings B = GetRegionBindings(store);
383 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
384 const BindingKey &K = I.getKey();
385 if (!K.isDirect())
386 continue;
387 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion())) {
388 // FIXME: Possibly incorporate the offset?
389 if (!f.HandleBinding(*this, store, R, I.getData()))
390 return;
391 }
392 }
Ted Kremenek609df302009-06-17 22:02:04 +0000393 }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000394};
395
396} // end anonymous namespace
397
Ted Kremenek4533a552009-06-16 22:36:44 +0000398//===----------------------------------------------------------------------===//
399// RegionStore creation.
400//===----------------------------------------------------------------------===//
401
402StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
403 RegionStoreFeatures F = maximal_features_tag();
404 return new RegionStoreManager(StMgr, F);
405}
406
407StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
408 RegionStoreFeatures F = minimal_features_tag();
409 F.enableFields(true);
410 return new RegionStoreManager(StMgr, F);
Ted Kremenek6779f892008-10-24 01:04:59 +0000411}
412
Ted Kremenekfa417142009-08-06 01:20:57 +0000413
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000414RegionStoreSubRegionMap*
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000415RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
416 RegionBindings B = GetRegionBindings(store);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000417 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump11289f42009-09-09 15:08:12 +0000418
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000419 llvm::SmallVector<const SubRegion*, 10> WL;
420
Ted Kremenek2c85f172009-08-06 04:50:20 +0000421 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek8e994a22010-01-11 00:07:44 +0000422 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremenekfa417142009-08-06 01:20:57 +0000423 M->process(WL, R);
Mike Stump11289f42009-09-09 15:08:12 +0000424
Mike Stump11289f42009-09-09 15:08:12 +0000425 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000426 // don't have direct bindings but are super regions of those that do.
427 while (!WL.empty()) {
428 const SubRegion *R = WL.back();
429 WL.pop_back();
Ted Kremenekfa417142009-08-06 01:20:57 +0000430 M->process(WL, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000431 }
432
Ted Kremenek9f276d62009-03-03 19:02:42 +0000433 return M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000434}
Ted Kremenek2907ab72008-12-24 07:46:32 +0000435
Ted Kremenek4533a552009-06-16 22:36:44 +0000436//===----------------------------------------------------------------------===//
Ted Kremeneka2536b62010-03-10 07:19:59 +0000437// Region Cluster analysis.
438//===----------------------------------------------------------------------===//
439
440namespace {
Ted Kremenekddce4a32010-03-10 16:32:56 +0000441template <typename DERIVED>
Ted Kremeneka2536b62010-03-10 07:19:59 +0000442class ClusterAnalysis {
443protected:
444 typedef BumpVector<BindingKey> RegionCluster;
445 typedef llvm::DenseMap<const MemRegion *, RegionCluster *> ClusterMap;
Ted Kremenekddce4a32010-03-10 16:32:56 +0000446 llvm::DenseMap<const RegionCluster*, unsigned> Visited;
447 typedef llvm::SmallVector<std::pair<const MemRegion *, RegionCluster*>, 10>
448 WorkList;
Ted Kremeneka2536b62010-03-10 07:19:59 +0000449
450 BumpVectorContext BVC;
451 ClusterMap ClusterM;
Ted Kremenekddce4a32010-03-10 16:32:56 +0000452 WorkList WL;
Ted Kremeneka2536b62010-03-10 07:19:59 +0000453
454 RegionStoreManager &RM;
455 ASTContext &Ctx;
456 ValueManager &ValMgr;
457
Ted Kremenekddce4a32010-03-10 16:32:56 +0000458 RegionBindings B;
459
Ted Kremeneka2536b62010-03-10 07:19:59 +0000460public:
Ted Kremenekddce4a32010-03-10 16:32:56 +0000461 ClusterAnalysis(RegionStoreManager &rm, GRStateManager &StateMgr,
462 RegionBindings b)
463 : RM(rm), Ctx(StateMgr.getContext()), ValMgr(StateMgr.getValueManager()),
464 B(b) {}
Ted Kremeneka2536b62010-03-10 07:19:59 +0000465
Ted Kremenekddce4a32010-03-10 16:32:56 +0000466 RegionBindings getRegionBindings() const { return B; }
Ted Kremeneka2536b62010-03-10 07:19:59 +0000467
Ted Kremenekbd862712010-07-01 20:16:50 +0000468 RegionCluster &AddToCluster(BindingKey K) {
Ted Kremenekddce4a32010-03-10 16:32:56 +0000469 const MemRegion *R = K.getRegion();
470 const MemRegion *baseR = R->getBaseRegion();
471 RegionCluster &C = getCluster(baseR);
472 C.push_back(K, BVC);
473 static_cast<DERIVED*>(this)->VisitAddedToCluster(baseR, C);
Ted Kremenekbd862712010-07-01 20:16:50 +0000474 return C;
Ted Kremeneka2536b62010-03-10 07:19:59 +0000475 }
Ted Kremeneka2536b62010-03-10 07:19:59 +0000476
Ted Kremenekddce4a32010-03-10 16:32:56 +0000477 bool isVisited(const MemRegion *R) {
478 return (bool) Visited[&getCluster(R->getBaseRegion())];
479 }
480
481 RegionCluster& getCluster(const MemRegion *R) {
482 RegionCluster *&CRef = ClusterM[R];
483 if (!CRef) {
484 void *Mem = BVC.getAllocator().template Allocate<RegionCluster>();
485 CRef = new (Mem) RegionCluster(BVC, 10);
486 }
487 return *CRef;
488 }
489
Ted Kremenekbd862712010-07-01 20:16:50 +0000490 void GenerateClusters(bool includeGlobals = false) {
Ted Kremenekddce4a32010-03-10 16:32:56 +0000491 // Scan the entire set of bindings and make the region clusters.
492 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekbd862712010-07-01 20:16:50 +0000493 RegionCluster &C = AddToCluster(RI.getKey());
Ted Kremenekddce4a32010-03-10 16:32:56 +0000494 if (const MemRegion *R = RI.getData().getAsRegion()) {
495 // Generate a cluster, but don't add the region to the cluster
496 // if there aren't any bindings.
497 getCluster(R->getBaseRegion());
498 }
Ted Kremenekbd862712010-07-01 20:16:50 +0000499 if (includeGlobals) {
500 const MemRegion *R = RI.getKey().getRegion();
501 if (isa<NonStaticGlobalSpaceRegion>(R->getMemorySpace()))
502 AddToWorkList(R, C);
503 }
Ted Kremeneka2536b62010-03-10 07:19:59 +0000504 }
505 }
Ted Kremenekddce4a32010-03-10 16:32:56 +0000506
507 bool AddToWorkList(const MemRegion *R, RegionCluster &C) {
508 if (unsigned &visited = Visited[&C])
509 return false;
510 else
511 visited = 1;
512
513 WL.push_back(std::make_pair(R, &C));
514 return true;
515 }
516
517 bool AddToWorkList(BindingKey K) {
518 return AddToWorkList(K.getRegion());
519 }
520
521 bool AddToWorkList(const MemRegion *R) {
522 const MemRegion *baseR = R->getBaseRegion();
523 return AddToWorkList(baseR, getCluster(baseR));
524 }
525
526 void RunWorkList() {
527 while (!WL.empty()) {
528 const MemRegion *baseR;
529 RegionCluster *C;
530 llvm::tie(baseR, C) = WL.back();
531 WL.pop_back();
532
533 // First visit the cluster.
534 static_cast<DERIVED*>(this)->VisitCluster(baseR, C->begin(), C->end());
535
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +0000536 // Next, visit the base region.
537 static_cast<DERIVED*>(this)->VisitBaseRegion(baseR);
Ted Kremenekddce4a32010-03-10 16:32:56 +0000538 }
539 }
540
541public:
542 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C) {}
543 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E) {}
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +0000544 void VisitBaseRegion(const MemRegion *baseR) {}
Ted Kremenekddce4a32010-03-10 16:32:56 +0000545};
Ted Kremeneka2536b62010-03-10 07:19:59 +0000546}
547
548//===----------------------------------------------------------------------===//
Ted Kremenekbca70672009-07-29 18:16:25 +0000549// Binding invalidation.
550//===----------------------------------------------------------------------===//
551
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000552void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
553 const MemRegion *R,
554 RegionStoreSubRegionMap &M) {
Ted Kremeneka2536b62010-03-10 07:19:59 +0000555
Ted Kremenekb251eb62010-02-02 22:38:47 +0000556 if (const RegionStoreSubRegionMap::Set *S = M.getSubRegions(R))
557 for (RegionStoreSubRegionMap::Set::iterator I = S->begin(), E = S->end();
558 I != E; ++I)
559 RemoveSubRegionBindings(B, *I, M);
Ted Kremeneka2536b62010-03-10 07:19:59 +0000560
Ted Kremenek8e994a22010-01-11 00:07:44 +0000561 B = Remove(B, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000562}
563
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000564namespace {
Ted Kremenekddce4a32010-03-10 16:32:56 +0000565class InvalidateRegionsWorker : public ClusterAnalysis<InvalidateRegionsWorker>
566{
567 const Expr *Ex;
568 unsigned Count;
Ted Kremenek7950b782010-02-13 00:54:03 +0000569 StoreManager::InvalidatedSymbols *IS;
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000570 StoreManager::InvalidatedRegions *Regions;
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000571public:
Ted Kremenek1a6672a2010-02-13 01:52:33 +0000572 InvalidateRegionsWorker(RegionStoreManager &rm,
Ted Kremenekddce4a32010-03-10 16:32:56 +0000573 GRStateManager &stateMgr,
574 RegionBindings b,
575 const Expr *ex, unsigned count,
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000576 StoreManager::InvalidatedSymbols *is,
577 StoreManager::InvalidatedRegions *r)
Ted Kremenekddce4a32010-03-10 16:32:56 +0000578 : ClusterAnalysis<InvalidateRegionsWorker>(rm, stateMgr, b),
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000579 Ex(ex), Count(count), IS(is), Regions(r) {}
Ted Kremeneka2536b62010-03-10 07:19:59 +0000580
Ted Kremenekddce4a32010-03-10 16:32:56 +0000581 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +0000582 void VisitBaseRegion(const MemRegion *baseR);
Ted Kremeneka2536b62010-03-10 07:19:59 +0000583
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000584private:
Ted Kremenek7950b782010-02-13 00:54:03 +0000585 void VisitBinding(SVal V);
Ted Kremeneka2536b62010-03-10 07:19:59 +0000586};
Ted Kremenekd9605642010-02-03 04:16:00 +0000587}
588
Ted Kremenek7950b782010-02-13 00:54:03 +0000589void InvalidateRegionsWorker::VisitBinding(SVal V) {
Ted Kremenek7950b782010-02-13 00:54:03 +0000590 // A symbol? Mark it touched by the invalidation.
591 if (IS)
592 if (SymbolRef Sym = V.getAsSymbol())
593 IS->insert(Sym);
Ted Kremeneka2536b62010-03-10 07:19:59 +0000594
Ted Kremenek1a6672a2010-02-13 01:52:33 +0000595 if (const MemRegion *R = V.getAsRegion()) {
596 AddToWorkList(R);
597 return;
598 }
599
600 // Is it a LazyCompoundVal? All references get invalidated as well.
601 if (const nonloc::LazyCompoundVal *LCS =
602 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
603
604 const MemRegion *LazyR = LCS->getRegion();
605 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
606
607 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekda534382010-07-06 23:53:29 +0000608 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
609 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenek1a6672a2010-02-13 01:52:33 +0000610 VisitBinding(RI.getData());
611 }
612
613 return;
614 }
615}
616
Ted Kremenekddce4a32010-03-10 16:32:56 +0000617void InvalidateRegionsWorker::VisitCluster(const MemRegion *baseR,
618 BindingKey *I, BindingKey *E) {
619 for ( ; I != E; ++I) {
620 // Get the old binding. Is it a region? If so, add it to the worklist.
621 const BindingKey &K = *I;
622 if (const SVal *V = RM.Lookup(B, K))
623 VisitBinding(*V);
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000624
Ted Kremenekddce4a32010-03-10 16:32:56 +0000625 B = RM.Remove(B, K);
626 }
627}
Ted Kremeneka2536b62010-03-10 07:19:59 +0000628
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +0000629void InvalidateRegionsWorker::VisitBaseRegion(const MemRegion *baseR) {
Ted Kremenekddce4a32010-03-10 16:32:56 +0000630 if (IS) {
631 // Symbolic region? Mark that symbol touched by the invalidation.
632 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR))
633 IS->insert(SR->getSymbol());
Ted Kremenekfa417142009-08-06 01:20:57 +0000634 }
635
Ted Kremenekddce4a32010-03-10 16:32:56 +0000636 // BlockDataRegion? If so, invalidate captured variables that are passed
637 // by reference.
638 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
639 for (BlockDataRegion::referenced_vars_iterator
640 BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
641 BI != BE; ++BI) {
642 const VarRegion *VR = *BI;
643 const VarDecl *VD = VR->getDecl();
644 if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
645 AddToWorkList(VR);
646 }
647 return;
648 }
649
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000650 // Otherwise, we have a normal data region. Record that we touched the region.
651 if (Regions)
652 Regions->push_back(baseR);
653
Ted Kremenekddce4a32010-03-10 16:32:56 +0000654 if (isa<AllocaRegion>(baseR) || isa<SymbolicRegion>(baseR)) {
655 // Invalidate the region by setting its default value to
656 // conjured symbol. The type of the symbol is irrelavant.
657 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
658 Count);
659 B = RM.Add(B, baseR, BindingKey::Default, V);
660 return;
661 }
662
663 if (!baseR->isBoundable())
664 return;
665
666 const TypedRegion *TR = cast<TypedRegion>(baseR);
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000667 QualType T = TR->getValueType();
Ted Kremenekddce4a32010-03-10 16:32:56 +0000668
669 // Invalidate the binding.
Zhongxing Xucdadf8d2010-08-21 06:51:45 +0000670 if (T->isStructureType()) {
Zhongxing Xuadc3ac72010-08-21 06:26:59 +0000671 // Invalidate the region by setting its default value to
672 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenekddce4a32010-03-10 16:32:56 +0000673 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, Ctx.IntTy,
674 Count);
675 B = RM.Add(B, baseR, BindingKey::Default, V);
676 return;
677 }
678
679 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
680 // Set the default value of the array to conjured symbol.
681 DefinedOrUnknownSVal V =
682 ValMgr.getConjuredSymbolVal(baseR, Ex, AT->getElementType(), Count);
683 B = RM.Add(B, baseR, BindingKey::Default, V);
684 return;
685 }
686
687 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(baseR, Ex, T, Count);
688 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
689 B = RM.Add(B, baseR, BindingKey::Direct, V);
Ted Kremenekbca70672009-07-29 18:16:25 +0000690}
691
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +0000692Store RegionStoreManager::InvalidateRegions(Store store,
693 const MemRegion * const *I,
694 const MemRegion * const *E,
695 const Expr *Ex, unsigned Count,
Ted Kremenekbd862712010-07-01 20:16:50 +0000696 InvalidatedSymbols *IS,
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000697 bool invalidateGlobals,
698 InvalidatedRegions *Regions) {
Ted Kremenekddce4a32010-03-10 16:32:56 +0000699 InvalidateRegionsWorker W(*this, StateMgr,
700 RegionStoreManager::GetRegionBindings(store),
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000701 Ex, Count, IS, Regions);
Ted Kremenekddce4a32010-03-10 16:32:56 +0000702
703 // Scan the bindings and generate the clusters.
Ted Kremenekbd862712010-07-01 20:16:50 +0000704 W.GenerateClusters(invalidateGlobals);
Ted Kremenekddce4a32010-03-10 16:32:56 +0000705
706 // Add I .. E to the worklist.
707 for ( ; I != E; ++I)
708 W.AddToWorkList(*I);
709
710 W.RunWorkList();
711
712 // Return the new bindings.
Ted Kremenekbd862712010-07-01 20:16:50 +0000713 RegionBindings B = W.getRegionBindings();
714
715 if (invalidateGlobals) {
716 // Bind the non-static globals memory space to a new symbol that we will
717 // use to derive the bindings for all non-static globals.
718 const GlobalsSpaceRegion *GS = MRMgr.getGlobalsRegion();
719 SVal V =
720 ValMgr.getConjuredSymbolVal(/* SymbolTag = */ (void*) GS, Ex,
721 /* symbol type, doesn't matter */ Ctx.IntTy,
722 Count);
723 B = Add(B, BindingKey::Make(GS, BindingKey::Default), V);
Jordy Roseac0ab20e2010-08-14 20:44:32 +0000724
725 // Even if there are no bindings in the global scope, we still need to
726 // record that we touched it.
727 if (Regions)
728 Regions->push_back(GS);
Ted Kremenekbd862712010-07-01 20:16:50 +0000729 }
730
731 return B.getRoot();
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000732}
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000733
Ted Kremenek4533a552009-06-16 22:36:44 +0000734//===----------------------------------------------------------------------===//
735// Extents for regions.
736//===----------------------------------------------------------------------===//
737
Zhongxing Xu383c2732009-11-12 02:48:32 +0000738DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
Zhongxing Xu228b0d42010-01-18 08:54:31 +0000739 const MemRegion *R,
740 QualType EleTy) {
Jordy Rose674bd552010-07-04 00:00:41 +0000741 SVal Size = cast<SubRegion>(R)->getExtent(ValMgr);
742 SValuator &SVator = ValMgr.getSValuator();
743 const llvm::APSInt *SizeInt = SVator.getKnownValue(state, Size);
744 if (!SizeInt)
745 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000746
Jordy Rose674bd552010-07-04 00:00:41 +0000747 CharUnits RegionSize = CharUnits::fromQuantity(SizeInt->getSExtValue());
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000748 CharUnits EleSize = Ctx.getTypeSizeInChars(EleTy);
Mike Stump11289f42009-09-09 15:08:12 +0000749
Jordy Rose674bd552010-07-04 00:00:41 +0000750 // If a variable is reinterpreted as a type that doesn't fit into a larger
751 // type evenly, round it down.
752 // This is a signed value, since it's used in arithmetic with signed indices.
753 return ValMgr.makeIntVal(RegionSize / EleSize, false);
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000754}
755
Ted Kremenek4533a552009-06-16 22:36:44 +0000756//===----------------------------------------------------------------------===//
757// Location and region casting.
758//===----------------------------------------------------------------------===//
759
Ted Kremenek2907ab72008-12-24 07:46:32 +0000760/// ArrayToPointer - Emulates the "decay" of an array to a pointer
761/// type. 'Array' represents the lvalue of the array being decayed
762/// to a pointer, and the returned SVal represents the decayed
763/// version of that lvalue (i.e., a pointer to the first element of
764/// the array). This is called by GRExprEngine when evaluating casts
765/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000766SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000767 if (!isa<loc::MemRegionVal>(Array))
768 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000769
Ted Kremenekf065b152008-12-13 19:24:37 +0000770 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
771 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000772
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000773 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000774 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000775
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000776 // Strip off typedefs from the ArrayRegion's ValueType.
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000777 QualType T = ArrayR->getValueType().getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000778 ArrayType *AT = cast<ArrayType>(T);
779 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000780
Ted Kremenekccc22922009-07-16 00:00:11 +0000781 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000782 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR, Ctx));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000783}
784
Ted Kremenek4533a552009-06-16 22:36:44 +0000785//===----------------------------------------------------------------------===//
786// Pointer arithmetic.
787//===----------------------------------------------------------------------===//
788
Zhongxing Xu0d081f32010-02-05 05:24:20 +0000789SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000790 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000791 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000792 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000793 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000794
Jordy Rose61176892010-06-28 08:26:15 +0000795 // Special case for zero RHS.
796 if (R.isZeroConstant()) {
797 switch (Op) {
798 default:
799 // Handle it normally.
800 break;
John McCalle3027922010-08-25 11:45:40 +0000801 case BO_Add:
802 case BO_Sub:
Jordy Rose61176892010-06-28 08:26:15 +0000803 // FIXME: does this need to be casted to match resultTy?
804 return L;
805 }
806 }
807
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000808 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000809 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000810
Ted Kremenekf6f04612009-07-11 00:58:27 +0000811 switch (MR->getKind()) {
812 case MemRegion::SymbolicRegionKind: {
813 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000814 SymbolRef Sym = SR->getSymbol();
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000815 QualType T = Sym->getType(Ctx);
Ted Kremenekd1d60662009-08-25 22:55:09 +0000816 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000817
Ted Kremenekd1d60662009-08-25 22:55:09 +0000818 if (const PointerType *PT = T->getAs<PointerType>())
819 EleTy = PT->getPointeeType();
820 else
John McCall9dd450b2009-09-21 23:43:11 +0000821 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000822
Ted Kremenekf6f04612009-07-11 00:58:27 +0000823 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000824 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000825 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000826 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000827 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000828 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000829 QualType EleTy = Ctx.CharTy; // Create an ElementRegion of bytes.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000830 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000831 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, Ctx);
Mike Stump11289f42009-09-09 15:08:12 +0000832 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000833 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000834
Ted Kremenekf6f04612009-07-11 00:58:27 +0000835 case MemRegion::ElementRegionKind: {
836 ER = cast<ElementRegion>(MR);
837 break;
838 }
Mike Stump11289f42009-09-09 15:08:12 +0000839
Ted Kremenekf6f04612009-07-11 00:58:27 +0000840 // Not yet handled.
841 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000842 case MemRegion::StringRegionKind: {
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000843
Ted Kremenek8ec57712009-10-06 01:39:48 +0000844 }
845 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000846 case MemRegion::CompoundLiteralRegionKind:
847 case MemRegion::FieldRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000848 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000849 case MemRegion::CXXObjectRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000850 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000851
Ted Kremenek10a50e72009-11-25 01:32:22 +0000852 case MemRegion::FunctionTextRegionKind:
853 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000854 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000855 // Technically this can happen if people do funny things with casts.
856 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenekacd71a42010-01-05 02:18:06 +0000858 case MemRegion::CXXThisRegionKind:
859 assert(0 &&
860 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek04af9f22009-12-07 22:05:27 +0000861 case MemRegion::GenericMemSpaceRegionKind:
862 case MemRegion::StackLocalsSpaceRegionKind:
863 case MemRegion::StackArgumentsSpaceRegionKind:
864 case MemRegion::HeapSpaceRegionKind:
Ted Kremenekbd862712010-07-01 20:16:50 +0000865 case MemRegion::NonStaticGlobalSpaceRegionKind:
866 case MemRegion::StaticGlobalSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000867 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000868 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
869 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000870 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000871
Zhongxing Xue7d14932009-03-02 07:52:23 +0000872 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000873 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +0000874
Ted Kremenek8ec57712009-10-06 01:39:48 +0000875 // For now, only support:
876 // (a) concrete integer indices that can easily be resolved
877 // (b) 0 + symbolic index
878 if (Base) {
879 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
880 // FIXME: Should use SValuator here.
881 SVal NewIdx =
882 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000883 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +0000884 const MemRegion* NewER =
885 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000886 ER->getSuperRegion(), Ctx);
Ted Kremenek8ec57712009-10-06 01:39:48 +0000887 return ValMgr.makeLoc(NewER);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000888 }
Ted Kremenek8ec57712009-10-06 01:39:48 +0000889 if (0 == Base->getValue()) {
890 const MemRegion* NewER =
891 MRMgr.getElementRegion(ER->getElementType(), R,
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000892 ER->getSuperRegion(), Ctx);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000893 return ValMgr.makeLoc(NewER);
894 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000895 }
Mike Stump11289f42009-09-09 15:08:12 +0000896
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000897 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000898}
899
Ted Kremenek4533a552009-06-16 22:36:44 +0000900//===----------------------------------------------------------------------===//
901// Loading values from regions.
902//===----------------------------------------------------------------------===//
903
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000904Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
Zhongxing Xu928a1902010-05-29 06:23:24 +0000905 const MemRegion *R) {
Zhongxing Xu94aec932010-05-29 06:49:04 +0000906
907 if (const SVal *V = Lookup(B, R, BindingKey::Direct))
908 return *V;
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000909
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000910 return Optional<SVal>();
911}
912
913Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000914 const MemRegion *R) {
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000915 if (R->isBoundable())
916 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000917 if (TR->getValueType()->isUnionType())
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000918 return UnknownVal();
919
Ted Kremenek64efd0d2010-02-03 03:06:46 +0000920 if (const SVal *V = Lookup(B, R, BindingKey::Default))
921 return *V;
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000922
923 return Optional<SVal>();
924}
925
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000926SVal RegionStoreManager::Retrieve(Store store, Loc L, QualType T) {
Zhongxing Xu83aff702008-10-21 05:29:26 +0000927 assert(!isa<UnknownVal>(L) && "location unknown");
928 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000929
Ted Kremenek2907ab72008-12-24 07:46:32 +0000930 // FIXME: Is this even possible? Shouldn't this be treated as a null
931 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000932 if (isa<loc::ConcreteInt>(L))
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000933 return UndefinedVal();
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000934
Ted Kremenek609df302009-06-17 22:02:04 +0000935 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000936
Tom Care375387d12010-06-25 18:22:31 +0000937 if (isa<AllocaRegion>(MR) || isa<SymbolicRegion>(MR)) {
938 if (T.isNull()) {
939 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +0000940 T = SR->getSymbol()->getType(Ctx);
Tom Care375387d12010-06-25 18:22:31 +0000941 }
Zhongxing Xu8ca2dc02010-02-08 08:43:02 +0000942 MR = GetElementZeroRegion(MR, T);
Tom Care375387d12010-06-25 18:22:31 +0000943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
Zhongxing Xub0e5c272010-03-01 05:29:02 +0000945 if (isa<CodeTextRegion>(MR)) {
946 assert(0 && "Why load from a code text region?");
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000947 return UnknownVal();
Zhongxing Xub0e5c272010-03-01 05:29:02 +0000948 }
Mike Stump11289f42009-09-09 15:08:12 +0000949
Ted Kremenek2907ab72008-12-24 07:46:32 +0000950 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
951 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +0000952 const TypedRegion *R = cast<TypedRegion>(MR);
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +0000953 QualType RTy = R->getValueType();
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000954
Ted Kremenek2907ab72008-12-24 07:46:32 +0000955 // FIXME: We should eventually handle funny addressing. e.g.:
956 //
957 // int x = ...;
958 // int *p = &x;
959 // char *q = (char*) p;
960 // char c = *q; // returns the first byte of 'x'.
961 //
962 // Such funny addressing will occur due to layering of regions.
963
Douglas Gregor8385a062010-04-26 21:31:17 +0000964 if (RTy->isStructureOrClassType())
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000965 return RetrieveStruct(store, R);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000967 // FIXME: Handle unions.
968 if (RTy->isUnionType())
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000969 return UnknownVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +0000970
971 if (RTy->isArrayType())
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000972 return RetrieveArray(store, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +0000973
Zhongxing Xuce270a62009-03-09 09:15:51 +0000974 // FIXME: handle Vector types.
975 if (RTy->isVectorType())
Zhongxing Xu4f8b9892010-02-04 02:39:47 +0000976 return UnknownVal();
Zhongxing Xu0628f532009-06-28 14:16:39 +0000977
978 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000979 return CastRetrievedVal(RetrieveField(store, FR), FR, T, false);
Zhongxing Xu0628f532009-06-28 14:16:39 +0000980
Ted Kremenekbe909b52010-01-11 02:33:26 +0000981 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R)) {
982 // FIXME: Here we actually perform an implicit conversion from the loaded
983 // value to the element type. Eventually we want to compose these values
984 // more intelligently. For example, an 'element' can encompass multiple
985 // bound regions (e.g., several bound bytes), or could be a subset of
986 // a larger value.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000987 return CastRetrievedVal(RetrieveElement(store, ER), ER, T, false);
Ted Kremenekba2e6c62010-03-10 07:20:03 +0000988 }
Mike Stump11289f42009-09-09 15:08:12 +0000989
Ted Kremenekbe909b52010-01-11 02:33:26 +0000990 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
991 // FIXME: Here we actually perform an implicit conversion from the loaded
992 // value to the ivar type. What we should model is stores to ivars
993 // that blow past the extent of the ivar. If the address of the ivar is
994 // reinterpretted, it is possible we stored a different value that could
995 // fit within the ivar. Either we need to cast these when storing them
996 // or reinterpret them lazily (as we do here).
Zhongxing Xuc7b9f952010-02-05 03:01:53 +0000997 return CastRetrievedVal(RetrieveObjCIvar(store, IVR), IVR, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +0000998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Ted Kremenekbe909b52010-01-11 02:33:26 +00001000 if (const VarRegion *VR = dyn_cast<VarRegion>(R)) {
1001 // FIXME: Here we actually perform an implicit conversion from the loaded
1002 // value to the variable type. What we should model is stores to variables
1003 // that blow past the extent of the variable. If the address of the
1004 // variable is reinterpretted, it is possible we stored a different value
1005 // that could fit within the variable. Either we need to cast these when
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001006 // storing them or reinterpret them lazily (as we do here).
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001007 return CastRetrievedVal(RetrieveVar(store, VR), VR, T, false);
Ted Kremenekbe909b52010-01-11 02:33:26 +00001008 }
Ted Kremenek834e2f62009-07-20 22:58:02 +00001009
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001010 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001011 const SVal *V = Lookup(B, R, BindingKey::Direct);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001012
1013 // Check if the region has a binding.
1014 if (V)
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001015 return *V;
Ted Kremenek2907ab72008-12-24 07:46:32 +00001016
Ted Kremenek2907ab72008-12-24 07:46:32 +00001017 // The location does not have a bound value. This means that it has
1018 // the value it had upon its creation and/or entry to the analyzed
1019 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekacd71a42010-01-05 02:18:06 +00001020 if (R->hasStackNonParametersStorage()) {
Ted Kremenek2907ab72008-12-24 07:46:32 +00001021 // All stack variables are considered to have undefined values
1022 // upon creation. All heap allocated blocks are considered to
1023 // have undefined values as well unless they are explicitly bound
1024 // to specific values.
Zhongxing Xu4f8b9892010-02-04 02:39:47 +00001025 return UndefinedVal();
Ted Kremenek2907ab72008-12-24 07:46:32 +00001026 }
1027
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001028 // All other values are symbolic.
Zhongxing Xu6d3cc382010-03-01 06:56:52 +00001029 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xu83aff702008-10-21 05:29:26 +00001030}
Mike Stump11289f42009-09-09 15:08:12 +00001031
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001032std::pair<Store, const MemRegion *>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001033RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001034 if (Optional<SVal> OV = getDirectBinding(B, R))
1035 if (const nonloc::LazyCompoundVal *V =
1036 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001037 return std::make_pair(V->getStore(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001038
Ted Kremenekfa417142009-08-06 01:20:57 +00001039 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001040 const std::pair<Store, const MemRegion *> &X =
Ted Kremenekfa417142009-08-06 01:20:57 +00001041 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001042
Ted Kremenek499b4e32010-02-09 19:11:53 +00001043 if (X.second)
Ted Kremenekfa417142009-08-06 01:20:57 +00001044 return std::make_pair(X.first,
1045 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001046 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001047 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001048 const std::pair<Store, const MemRegion *> &X =
Ted Kremenekfa417142009-08-06 01:20:57 +00001049 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001050
Ted Kremenek499b4e32010-02-09 19:11:53 +00001051 if (X.second)
Ted Kremenekfa417142009-08-06 01:20:57 +00001052 return std::make_pair(X.first,
1053 MRMgr.getFieldRegionWithSuper(FR, X.second));
1054 }
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001055 // The NULL MemRegion indicates an non-existent lazy binding. A NULL Store is
Zhongxing Xubc1d4ae2010-02-10 02:02:10 +00001056 // possible for a valid lazy binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001057 return std::make_pair((Store) 0, (const MemRegion *) 0);
Ted Kremenekfa417142009-08-06 01:20:57 +00001058}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001059
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001060SVal RegionStoreManager::RetrieveElement(Store store,
Zhongxing Xu2d160732009-06-25 05:29:39 +00001061 const ElementRegion* R) {
1062 // Check if the region has a binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001063 RegionBindings B = GetRegionBindings(store);
Ted Kremenekf7c226d2010-03-30 20:30:52 +00001064 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001065 return *V;
1066
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001067 const MemRegion* superR = R->getSuperRegion();
1068
Zhongxing Xu2d160732009-06-25 05:29:39 +00001069 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001070 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001071 // FIXME: Handle loads from strings where the literal is treated as
Ted Kremenek228539f2009-09-29 16:36:48 +00001072 // an integer, e.g., *((unsigned int*)"hello")
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001073 QualType T = Ctx.getAsArrayType(StrR->getValueType())->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001074 if (T != Ctx.getCanonicalType(R->getElementType()))
1075 return UnknownVal();
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001076
Zhongxing Xu2d160732009-06-25 05:29:39 +00001077 const StringLiteral *Str = StrR->getStringLiteral();
1078 SVal Idx = R->getIndex();
1079 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1080 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001081 int64_t byteLength = Str->getByteLength();
Jordy Rosedaa1c832010-07-29 06:40:33 +00001082 // Technically, only i == byteLength is guaranteed to be null.
1083 // However, such overflows should be caught before reaching this point;
1084 // the only time such an access would be made is if a string literal was
1085 // used to initialize a larger array.
Benjamin Kramer35b077e2010-08-17 12:54:38 +00001086 char c = (i >= byteLength) ? '\0' : Str->getString()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001087 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001088 }
1089 }
Ted Kremenekd588e782010-09-01 23:00:46 +00001090
1091 // Check for loads from a code text region. For such loads, just give up.
1092 if (const CodeTextRegion *cR = dyn_cast<CodeTextRegion>(superR))
1093 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001094
Ted Kremenek4708f5a2010-05-31 01:22:04 +00001095 // Handle the case where we are indexing into a larger scalar object.
1096 // For example, this handles:
1097 // int x = ...
1098 // char *y = &x;
1099 // return *y;
1100 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
Zhongxing Xufd91d272010-08-02 04:56:14 +00001101 const RegionRawOffset &O = R->getAsArrayOffset();
Ted Kremenek4708f5a2010-05-31 01:22:04 +00001102 if (const TypedRegion *baseR = dyn_cast_or_null<TypedRegion>(O.getRegion())) {
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001103 QualType baseT = baseR->getValueType();
Ted Kremenek4708f5a2010-05-31 01:22:04 +00001104 if (baseT->isScalarType()) {
1105 QualType elemT = R->getElementType();
1106 if (elemT->isScalarType()) {
1107 if (Ctx.getTypeSizeInChars(baseT) >= Ctx.getTypeSizeInChars(elemT)) {
1108 if (const Optional<SVal> &V = getDirectBinding(B, superR)) {
1109 if (SymbolRef parentSym = V->getAsSymbol())
1110 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenekbd862712010-07-01 20:16:50 +00001111
Ted Kremenek4708f5a2010-05-31 01:22:04 +00001112 if (V->isUnknownOrUndef())
1113 return *V;
1114 // Other cases: give up. We are indexing into a larger object
1115 // that has some value, but we don't know how to handle that yet.
1116 return UnknownVal();
1117 }
1118 }
1119 }
Zhongxing Xu94aec932010-05-29 06:49:04 +00001120 }
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001121 }
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001122 return RetrieveFieldOrElementCommon(store, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001123}
1124
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001125SVal RegionStoreManager::RetrieveField(Store store,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001126 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001127
1128 // Check if the region has a binding.
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001129 RegionBindings B = GetRegionBindings(store);
Ted Kremenekf7c226d2010-03-30 20:30:52 +00001130 if (const Optional<SVal> &V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001131 return *V;
1132
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001133 QualType Ty = R->getValueType();
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001134 return RetrieveFieldOrElementCommon(store, R, Ty, R->getSuperRegion());
Ted Kremenek040e3b92009-08-06 22:33:36 +00001135}
Mike Stump11289f42009-09-09 15:08:12 +00001136
Ted Kremenekbd862712010-07-01 20:16:50 +00001137Optional<SVal>
1138RegionStoreManager::RetrieveDerivedDefaultValue(RegionBindings B,
1139 const MemRegion *superR,
1140 const TypedRegion *R,
1141 QualType Ty) {
1142
1143 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
1144 if (SymbolRef parentSym = D->getAsSymbol())
1145 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1146
1147 if (D->isZeroConstant())
1148 return ValMgr.makeZeroVal(Ty);
1149
1150 if (D->isUnknownOrUndef())
1151 return *D;
1152
1153 assert(0 && "Unknown default value");
1154 }
1155
1156 return Optional<SVal>();
1157}
1158
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001159SVal RegionStoreManager::RetrieveFieldOrElementCommon(Store store,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001160 const TypedRegion *R,
1161 QualType Ty,
1162 const MemRegion *superR) {
1163
Mike Stump11289f42009-09-09 15:08:12 +00001164 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001165 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001166
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001167 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001168
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001169 while (superR) {
Ted Kremenekbd862712010-07-01 20:16:50 +00001170 if (const Optional<SVal> &D = RetrieveDerivedDefaultValue(B, superR, R, Ty))
1171 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001172
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001173 // If our super region is a field or element itself, walk up the region
1174 // hierarchy to see if there is a default value installed in an ancestor.
1175 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1176 superR = cast<SubRegion>(superR)->getSuperRegion();
1177 continue;
1178 }
Mike Stump11289f42009-09-09 15:08:12 +00001179
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001180 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001181 }
Mike Stump11289f42009-09-09 15:08:12 +00001182
Ted Kremenekfa417142009-08-06 01:20:57 +00001183 // Lazy binding?
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001184 Store lazyBindingStore = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001185 const MemRegion *lazyBindingRegion = NULL;
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001186 llvm::tie(lazyBindingStore, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001187
Ted Kremenek499b4e32010-02-09 19:11:53 +00001188 if (lazyBindingRegion) {
1189 if (const ElementRegion *ER = dyn_cast<ElementRegion>(lazyBindingRegion))
1190 return RetrieveElement(lazyBindingStore, ER);
Zhongxing Xubd96bf12010-02-05 02:26:30 +00001191 return RetrieveField(lazyBindingStore,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001192 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001193 }
1194
Ted Kremenekacd71a42010-01-05 02:18:06 +00001195 if (R->hasStackNonParametersStorage()) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001196 if (isa<ElementRegion>(R)) {
1197 // Currently we don't reason specially about Clang-style vectors. Check
1198 // if superR is a vector and if so return Unknown.
1199 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001200 if (typedSuperR->getValueType()->isVectorType())
Ted Kremenek040e3b92009-08-06 22:33:36 +00001201 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001202 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001205 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001206 }
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001208 // All other values are symbolic.
Zhongxing Xu6d3cc382010-03-01 06:56:52 +00001209 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001210}
Mike Stump11289f42009-09-09 15:08:12 +00001211
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001212SVal RegionStoreManager::RetrieveObjCIvar(Store store, const ObjCIvarRegion* R){
Ted Kremenek48029552009-07-15 06:09:28 +00001213
Ted Kremenek48029552009-07-15 06:09:28 +00001214 // Check if the region has a binding.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001215 RegionBindings B = GetRegionBindings(store);
Ted Kremenek48029552009-07-15 06:09:28 +00001216
Ted Kremenekf7c226d2010-03-30 20:30:52 +00001217 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001218 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001219
Ted Kremenek48029552009-07-15 06:09:28 +00001220 const MemRegion *superR = R->getSuperRegion();
1221
Ted Kremenek481c1212009-10-20 01:20:57 +00001222 // Check if the super region has a default binding.
Ted Kremenekf7c226d2010-03-30 20:30:52 +00001223 if (const Optional<SVal> &V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001224 if (SymbolRef parentSym = V->getAsSymbol())
1225 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001226
Ted Kremenek48029552009-07-15 06:09:28 +00001227 // Other cases: give up.
1228 return UnknownVal();
1229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001231 return RetrieveLazySymbol(R);
Ted Kremenek834e2f62009-07-20 22:58:02 +00001232}
1233
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001234SVal RegionStoreManager::RetrieveVar(Store store, const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001235
Ted Kremenekfe12f882009-07-21 00:12:07 +00001236 // Check if the region has a binding.
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001237 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001238
Ted Kremenekf7c226d2010-03-30 20:30:52 +00001239 if (const Optional<SVal> &V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001240 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001241
Ted Kremenekfe12f882009-07-21 00:12:07 +00001242 // Lazily derive a value for the VarRegion.
1243 const VarDecl *VD = R->getDecl();
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001244 QualType T = VD->getType();
1245 const MemSpaceRegion *MS = R->getMemorySpace();
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001246
1247 if (isa<UnknownSpaceRegion>(MS) ||
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001248 isa<StackArgumentsSpaceRegion>(MS))
Zhongxing Xu6d3cc382010-03-01 06:56:52 +00001249 return ValMgr.getRegionValueSymbolVal(R);
Mike Stump11289f42009-09-09 15:08:12 +00001250
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001251 if (isa<GlobalsSpaceRegion>(MS)) {
Ted Kremenekbd862712010-07-01 20:16:50 +00001252 if (isa<NonStaticGlobalSpaceRegion>(MS)) {
Ted Kremenek2d107f92010-03-30 20:31:04 +00001253 // Is 'VD' declared constant? If so, retrieve the constant value.
1254 QualType CT = Ctx.getCanonicalType(T);
1255 if (CT.isConstQualified()) {
1256 const Expr *Init = VD->getInit();
1257 // Do the null check first, as we want to call 'IgnoreParenCasts'.
1258 if (Init)
1259 if (const IntegerLiteral *IL =
1260 dyn_cast<IntegerLiteral>(Init->IgnoreParenCasts())) {
1261 const nonloc::ConcreteInt &V = ValMgr.makeIntVal(IL);
1262 return ValMgr.getSValuator().EvalCast(V, Init->getType(),
1263 IL->getType());
1264 }
1265 }
1266
Ted Kremenekbd862712010-07-01 20:16:50 +00001267 if (const Optional<SVal> &V = RetrieveDerivedDefaultValue(B, MS, R, CT))
1268 return V.getValue();
1269
Zhongxing Xu6d3cc382010-03-01 06:56:52 +00001270 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek2d107f92010-03-30 20:31:04 +00001271 }
Mike Stump11289f42009-09-09 15:08:12 +00001272
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001273 if (T->isIntegerType())
1274 return ValMgr.makeIntVal(0, T);
Ted Kremenekbdfcacb2010-02-06 04:04:46 +00001275 if (T->isPointerType())
1276 return ValMgr.makeNull();
1277
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001278 return UnknownVal();
Ted Kremenek30fe9ec2010-02-06 03:57:59 +00001279 }
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001280
Ted Kremenekfe12f882009-07-21 00:12:07 +00001281 return UndefinedVal();
1282}
1283
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001284SVal RegionStoreManager::RetrieveLazySymbol(const TypedRegion *R) {
Ted Kremenek48029552009-07-15 06:09:28 +00001285 // All other values are symbolic.
Zhongxing Xu6d3cc382010-03-01 06:56:52 +00001286 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek48029552009-07-15 06:09:28 +00001287}
1288
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001289SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001290 QualType T = R->getValueType();
Douglas Gregor8385a062010-04-26 21:31:17 +00001291 assert(T->isStructureOrClassType());
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001292 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001293}
1294
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001295SVal RegionStoreManager::RetrieveArray(Store store, const TypedRegion * R) {
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001296 assert(isa<ConstantArrayType>(R->getValueType()));
Zhongxing Xuc7b9f952010-02-05 03:01:53 +00001297 return ValMgr.makeLazyCompoundVal(store, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001298}
1299
Ted Kremenek4533a552009-06-16 22:36:44 +00001300//===----------------------------------------------------------------------===//
1301// Binding values to regions.
1302//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001303
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001304Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001305 if (isa<loc::MemRegionVal>(L))
Ted Kremenekbe909b52010-01-11 02:33:26 +00001306 if (const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion())
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001307 return Remove(GetRegionBindings(store), R).getRoot();
Mike Stump11289f42009-09-09 15:08:12 +00001308
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001309 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001310}
1311
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001312Store RegionStoreManager::Bind(Store store, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001313 if (isa<loc::ConcreteInt>(L))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001314 return store;
Zhongxing Xud260db12009-06-28 10:16:11 +00001315
Ted Kremenek4533a552009-06-16 22:36:44 +00001316 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001317 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001318
Ted Kremenek4533a552009-06-16 22:36:44 +00001319 // Check if the region is a struct region.
1320 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001321 if (TR->getValueType()->isStructureOrClassType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001322 return BindStruct(store, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001323
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001324 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1325 if (ER->getIndex().isZeroConstant()) {
1326 if (const TypedRegion *superR =
1327 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001328 QualType superTy = superR->getValueType();
Ted Kremenek25c9c142009-09-21 22:58:52 +00001329 // For now, just invalidate the fields of the struct/union/class.
Zhongxing Xue11f17f2010-08-21 11:03:37 +00001330 // This is for test rdar_test_7185607 in misc-ps-region-store.m.
Ted Kremenek25c9c142009-09-21 22:58:52 +00001331 // FIXME: Precisely handle the fields of the record.
Jordy Rosec88c74c2010-08-05 03:28:45 +00001332 if (superTy->isStructureOrClassType())
1333 return KillStruct(store, superR, UnknownVal());
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001334 }
1335 }
1336 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001337 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1338 // Binding directly to a symbolic region should be treated as binding
1339 // to element 0.
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +00001340 QualType T = SR->getSymbol()->getType(Ctx);
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001341
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001342 // FIXME: Is this the right way to handle symbols that are references?
1343 if (const PointerType *PT = T->getAs<PointerType>())
1344 T = PT->getPointeeType();
1345 else
1346 T = T->getAs<ReferenceType>()->getPointeeType();
1347
Ted Kremenek267e45a2009-09-24 04:11:44 +00001348 R = GetElementZeroRegion(SR, T);
1349 }
Mike Stump11289f42009-09-09 15:08:12 +00001350
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001351 // Perform the binding.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001352 RegionBindings B = GetRegionBindings(store);
1353 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek4533a552009-06-16 22:36:44 +00001354}
1355
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001356Store RegionStoreManager::BindDecl(Store store, const VarRegion *VR,
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001357 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001358
Ted Kremenekb006b822009-11-04 00:09:15 +00001359 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001360
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001361 if (T->isArrayType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001362 return BindArray(store, VR, InitVal);
Douglas Gregor8385a062010-04-26 21:31:17 +00001363 if (T->isStructureOrClassType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001364 return BindStruct(store, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001365
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001366 return Bind(store, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001367}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001368
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001369// FIXME: this method should be merged into Bind().
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001370Store RegionStoreManager::BindCompoundLiteral(Store store,
1371 const CompoundLiteralExpr *CL,
1372 const LocationContext *LC,
1373 SVal V) {
1374 return Bind(store, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
Ted Kremenek04af9f22009-12-07 22:05:27 +00001375 V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001376}
1377
Zhongxing Xu527ff6d2010-06-01 03:01:33 +00001378
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001379Store RegionStoreManager::setImplicitDefaultValue(Store store,
1380 const MemRegion *R,
1381 QualType T) {
Ted Kremenek439a6d12009-11-19 20:20:24 +00001382 RegionBindings B = GetRegionBindings(store);
1383 SVal V;
1384
1385 if (Loc::IsLocType(T))
1386 V = ValMgr.makeNull();
1387 else if (T->isIntegerType())
1388 V = ValMgr.makeZeroVal(T);
Douglas Gregor8385a062010-04-26 21:31:17 +00001389 else if (T->isStructureOrClassType() || T->isArrayType()) {
Ted Kremenek439a6d12009-11-19 20:20:24 +00001390 // Set the default value to a zero constant when it is a structure
1391 // or array. The type doesn't really matter.
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +00001392 V = ValMgr.makeZeroVal(Ctx.IntTy);
Ted Kremenek439a6d12009-11-19 20:20:24 +00001393 }
1394 else {
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001395 return store;
Ted Kremenek439a6d12009-11-19 20:20:24 +00001396 }
Ted Kremenek8e994a22010-01-11 00:07:44 +00001397
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001398 return Add(B, R, BindingKey::Default, V).getRoot();
Ted Kremenek439a6d12009-11-19 20:20:24 +00001399}
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001400
1401Store RegionStoreManager::BindArray(Store store, const TypedRegion* R,
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001402 SVal Init) {
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001403
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001404 const ArrayType *AT =cast<ArrayType>(Ctx.getCanonicalType(R->getValueType()));
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001405 QualType ElementTy = AT->getElementType();
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001406 Optional<uint64_t> Size;
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001407
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001408 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
1409 Size = CAT->getSize().getZExtValue();
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001410
Jordy Rosedaa1c832010-07-29 06:40:33 +00001411 // Check if the init expr is a string literal.
1412 if (loc::MemRegionVal *MRV = dyn_cast<loc::MemRegionVal>(&Init)) {
1413 const StringRegion *S = cast<StringRegion>(MRV->getRegion());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001414
Jordy Rosedaa1c832010-07-29 06:40:33 +00001415 // Treat the string as a lazy compound value.
1416 nonloc::LazyCompoundVal LCV =
1417 cast<nonloc::LazyCompoundVal>(ValMgr.makeLazyCompoundVal(store, S));
1418 return CopyLazyBindings(LCV, store, R);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001419 }
1420
Ted Kremenekfa417142009-08-06 01:20:57 +00001421 // Handle lazy compound values.
1422 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001423 return CopyLazyBindings(*LCV, store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001424
1425 // Remaining case: explicit compound values.
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001426
Ted Kremenek439a6d12009-11-19 20:20:24 +00001427 if (Init.isUnknown())
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001428 return setImplicitDefaultValue(store, R, ElementTy);
1429
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001430 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001431 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001432 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001433
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001434 for (; Size.hasValue() ? i < Size.getValue() : true ; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001435 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001436 if (VI == VE)
1437 break;
1438
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001439 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu0ba9fd62010-08-15 10:08:38 +00001440 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, Ctx);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001441
Douglas Gregor8385a062010-04-26 21:31:17 +00001442 if (ElementTy->isStructureOrClassType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001443 store = BindStruct(store, ER, *VI);
Jordy Rose796b5122010-08-20 01:05:59 +00001444 else if (ElementTy->isArrayType())
1445 store = BindArray(store, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001446 else
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001447 store = Bind(store, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001448 }
1449
Ted Kremenek439a6d12009-11-19 20:20:24 +00001450 // If the init list is shorter than the array length, set the
1451 // array default value.
Ted Kremeneke36bceb2010-01-26 23:51:00 +00001452 if (Size.hasValue() && i < Size.getValue())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001453 store = setImplicitDefaultValue(store, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001454
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001455 return store;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001456}
1457
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001458Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R,
1459 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001460
Ted Kremenek609df302009-06-17 22:02:04 +00001461 if (!Features.supportsFields())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001462 return store;
Mike Stump11289f42009-09-09 15:08:12 +00001463
Zhongxing Xu8de0a3d2010-08-11 06:10:55 +00001464 QualType T = R->getValueType();
Douglas Gregor8385a062010-04-26 21:31:17 +00001465 assert(T->isStructureOrClassType());
Zhongxing Xub393b502008-10-31 10:53:01 +00001466
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001467 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001468 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001469
1470 if (!RD->isDefinition())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001471 return store;
Zhongxing Xub393b502008-10-31 10:53:01 +00001472
Ted Kremenekfa417142009-08-06 01:20:57 +00001473 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001474 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001475 return CopyLazyBindings(*LCV, store, R);
Mike Stump11289f42009-09-09 15:08:12 +00001476
Ted Kremenek385f71b2010-07-29 00:28:47 +00001477 // We may get non-CompoundVal accidentally due to imprecise cast logic or
1478 // that we are binding symbolic struct value. Kill the field values, and if
1479 // the value is symbolic go and bind it as a "default" binding.
Ted Kremenek53221502010-08-17 23:29:06 +00001480 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V)) {
1481 SVal SV = isa<nonloc::SymbolVal>(V) ? V : UnknownVal();
1482 return KillStruct(store, R, SV);
1483 }
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001484
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001485 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001486 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001487
1488 RecordDecl::field_iterator FI, FE;
1489
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001490 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001491
Zhongxing Xu0442e962009-06-23 05:43:16 +00001492 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001493 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001494
Zhongxing Xub393b502008-10-31 10:53:01 +00001495 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001496 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001497
Ted Kremenek30030012009-09-22 21:19:14 +00001498 if (FTy->isArrayType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001499 store = BindArray(store, FR, *VI);
Douglas Gregor8385a062010-04-26 21:31:17 +00001500 else if (FTy->isStructureOrClassType())
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001501 store = BindStruct(store, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001502 else
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001503 store = Bind(store, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001504 }
1505
Zhongxing Xu0442e962009-06-23 05:43:16 +00001506 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001507 if (FI != FE) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001508 RegionBindings B = GetRegionBindings(store);
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001509 B = Add(B, R, BindingKey::Default, ValMgr.makeIntVal(0, false));
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001510 store = B.getRoot();
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001511 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001512
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001513 return store;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001514}
1515
Ted Kremenek385f71b2010-07-29 00:28:47 +00001516Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R,
1517 SVal DefaultVal) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001518 RegionBindings B = GetRegionBindings(store);
1519 llvm::OwningPtr<RegionStoreSubRegionMap>
1520 SubRegions(getRegionStoreSubRegionMap(store));
1521 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001522
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001523 // Set the default value of the struct region to "unknown".
Ted Kremenek385f71b2010-07-29 00:28:47 +00001524 return Add(B, R, BindingKey::Default, DefaultVal).getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001525}
1526
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001527Store RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1528 Store store, const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001529
Ted Kremenekfa417142009-08-06 01:20:57 +00001530 // Nuke the old bindings stemming from R.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001531 RegionBindings B = GetRegionBindings(store);
Ted Kremenekfa417142009-08-06 01:20:57 +00001532
Mike Stump11289f42009-09-09 15:08:12 +00001533 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001534 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekfa417142009-08-06 01:20:57 +00001535
Mike Stump11289f42009-09-09 15:08:12 +00001536 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001537 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001538
Ted Kremenekfa417142009-08-06 01:20:57 +00001539 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1540 // results in a zero-copy algorithm.
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001541 return Add(B, R, BindingKey::Direct, V).getRoot();
Ted Kremenek8e994a22010-01-11 00:07:44 +00001542}
1543
1544//===----------------------------------------------------------------------===//
1545// "Raw" retrievals and bindings.
1546//===----------------------------------------------------------------------===//
1547
Ted Kremenekbe909b52010-01-11 02:33:26 +00001548
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001549RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K, SVal V) {
Jordy Rose63a38a12010-08-16 20:53:01 +00001550 if (!K.isValid())
Jordy Rosedf28e8e2010-08-16 01:15:17 +00001551 return B;
Ted Kremenek8e994a22010-01-11 00:07:44 +00001552 return RBFactory.Add(B, K, V);
1553}
1554
1555RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001556 BindingKey::Kind k, SVal V) {
1557 return Add(B, BindingKey::Make(R, k), V);
Ted Kremenek8e994a22010-01-11 00:07:44 +00001558}
1559
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001560const SVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
Jordy Rose63a38a12010-08-16 20:53:01 +00001561 if (!K.isValid())
Jordy Rosedf28e8e2010-08-16 01:15:17 +00001562 return NULL;
Ted Kremenek8e994a22010-01-11 00:07:44 +00001563 return B.lookup(K);
1564}
1565
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001566const SVal *RegionStoreManager::Lookup(RegionBindings B,
1567 const MemRegion *R,
1568 BindingKey::Kind k) {
1569 return Lookup(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001570}
1571
1572RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
Jordy Rose63a38a12010-08-16 20:53:01 +00001573 if (!K.isValid())
Jordy Rosedf28e8e2010-08-16 01:15:17 +00001574 return B;
Ted Kremenek8e994a22010-01-11 00:07:44 +00001575 return RBFactory.Remove(B, K);
1576}
1577
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001578RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R,
1579 BindingKey::Kind k){
1580 return Remove(B, BindingKey::Make(R, k));
Ted Kremenek8e994a22010-01-11 00:07:44 +00001581}
1582
Ted Kremenek4533a552009-06-16 22:36:44 +00001583//===----------------------------------------------------------------------===//
1584// State pruning.
1585//===----------------------------------------------------------------------===//
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001586
Ted Kremenekddce4a32010-03-10 16:32:56 +00001587namespace {
1588class RemoveDeadBindingsWorker :
1589 public ClusterAnalysis<RemoveDeadBindingsWorker> {
1590 llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
1591 SymbolReaper &SymReaper;
Zhongxing Xu03fd7662010-03-17 03:35:08 +00001592 const StackFrameContext *CurrentLCtx;
Ted Kremenekbd862712010-07-01 20:16:50 +00001593
Ted Kremenekddce4a32010-03-10 16:32:56 +00001594public:
1595 RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
1596 RegionBindings b, SymbolReaper &symReaper,
Jordy Rosef7488ec2010-07-01 20:09:55 +00001597 const StackFrameContext *LCtx)
Ted Kremenekddce4a32010-03-10 16:32:56 +00001598 : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
Jordy Rosef7488ec2010-07-01 20:09:55 +00001599 SymReaper(symReaper), CurrentLCtx(LCtx) {}
Ted Kremenekddce4a32010-03-10 16:32:56 +00001600
1601 // Called by ClusterAnalysis.
1602 void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
1603 void VisitCluster(const MemRegion *baseR, BindingKey *I, BindingKey *E);
Ted Kremenekddce4a32010-03-10 16:32:56 +00001604
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001605 void VisitBindingKey(BindingKey K);
Ted Kremenekddce4a32010-03-10 16:32:56 +00001606 bool UpdatePostponed();
1607 void VisitBinding(SVal V);
1608};
1609}
1610
1611void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion *baseR,
1612 RegionCluster &C) {
1613
1614 if (const VarRegion *VR = dyn_cast<VarRegion>(baseR)) {
Jordy Rosef7488ec2010-07-01 20:09:55 +00001615 if (SymReaper.isLive(VR))
Ted Kremenekddce4a32010-03-10 16:32:56 +00001616 AddToWorkList(baseR, C);
1617
1618 return;
1619 }
1620
1621 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(baseR)) {
1622 if (SymReaper.isLive(SR->getSymbol()))
1623 AddToWorkList(SR, C);
1624 else
1625 Postponed.push_back(SR);
1626
1627 return;
1628 }
Zhongxing Xu03fd7662010-03-17 03:35:08 +00001629
Ted Kremenekbd862712010-07-01 20:16:50 +00001630 if (isa<NonStaticGlobalSpaceRegion>(baseR)) {
1631 AddToWorkList(baseR, C);
1632 return;
1633 }
1634
Zhongxing Xu03fd7662010-03-17 03:35:08 +00001635 // CXXThisRegion in the current or parent location context is live.
1636 if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
Ted Kremenekbd862712010-07-01 20:16:50 +00001637 const StackArgumentsSpaceRegion *StackReg =
Zhongxing Xu03fd7662010-03-17 03:35:08 +00001638 cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
1639 const StackFrameContext *RegCtx = StackReg->getStackFrame();
1640 if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
1641 AddToWorkList(TR, C);
1642 }
Ted Kremenekddce4a32010-03-10 16:32:56 +00001643}
1644
1645void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
1646 BindingKey *I, BindingKey *E) {
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001647 for ( ; I != E; ++I)
1648 VisitBindingKey(*I);
Ted Kremenekddce4a32010-03-10 16:32:56 +00001649}
1650
1651void RemoveDeadBindingsWorker::VisitBinding(SVal V) {
1652 // Is it a LazyCompoundVal? All referenced regions are live as well.
1653 if (const nonloc::LazyCompoundVal *LCS =
1654 dyn_cast<nonloc::LazyCompoundVal>(&V)) {
1655
1656 const MemRegion *LazyR = LCS->getRegion();
1657 RegionBindings B = RegionStoreManager::GetRegionBindings(LCS->getStore());
1658 for (RegionBindings::iterator RI = B.begin(), RE = B.end(); RI != RE; ++RI){
Ted Kremenekda534382010-07-06 23:53:29 +00001659 const SubRegion *baseR = dyn_cast<SubRegion>(RI.getKey().getRegion());
1660 if (baseR && baseR->isSubRegionOf(LazyR))
Ted Kremenekddce4a32010-03-10 16:32:56 +00001661 VisitBinding(RI.getData());
1662 }
1663 return;
1664 }
1665
1666 // If V is a region, then add it to the worklist.
1667 if (const MemRegion *R = V.getAsRegion())
1668 AddToWorkList(R);
1669
1670 // Update the set of live symbols.
1671 for (SVal::symbol_iterator SI=V.symbol_begin(), SE=V.symbol_end();
1672 SI!=SE;++SI)
1673 SymReaper.markLive(*SI);
1674}
1675
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001676void RemoveDeadBindingsWorker::VisitBindingKey(BindingKey K) {
1677 const MemRegion *R = K.getRegion();
1678
Ted Kremenekddce4a32010-03-10 16:32:56 +00001679 // Mark this region "live" by adding it to the worklist. This will cause
1680 // use to visit all regions in the cluster (if we haven't visited them
1681 // already).
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001682 if (AddToWorkList(R)) {
1683 // Mark the symbol for any live SymbolicRegion as "live". This means we
1684 // should continue to track that symbol.
1685 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
1686 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekddce4a32010-03-10 16:32:56 +00001687
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001688 // For BlockDataRegions, enqueue the VarRegions for variables marked
1689 // with __block (passed-by-reference).
1690 // via BlockDeclRefExprs.
1691 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1692 for (BlockDataRegion::referenced_vars_iterator
1693 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1694 RI != RE; ++RI) {
1695 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1696 AddToWorkList(*RI);
1697 }
Ted Kremenekddce4a32010-03-10 16:32:56 +00001698
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001699 // No possible data bindings on a BlockDataRegion.
1700 return;
Ted Kremenekddce4a32010-03-10 16:32:56 +00001701 }
Ted Kremenekddce4a32010-03-10 16:32:56 +00001702 }
1703
Ted Kremenekc3e1f2f2010-04-01 00:15:55 +00001704 // Visit the data binding for K.
1705 if (const SVal *V = RM.Lookup(B, K))
Ted Kremenekddce4a32010-03-10 16:32:56 +00001706 VisitBinding(*V);
1707}
1708
1709bool RemoveDeadBindingsWorker::UpdatePostponed() {
1710 // See if any postponed SymbolicRegions are actually live now, after
1711 // having done a scan.
1712 bool changed = false;
1713
1714 for (llvm::SmallVectorImpl<const SymbolicRegion*>::iterator
1715 I = Postponed.begin(), E = Postponed.end() ; I != E ; ++I) {
1716 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(*I)) {
1717 if (SymReaper.isLive(SR->getSymbol())) {
1718 changed |= AddToWorkList(SR);
1719 *I = NULL;
1720 }
1721 }
1722 }
1723
1724 return changed;
1725}
1726
Zhongxing Xuee770d42010-08-15 12:45:09 +00001727Store RegionStoreManager::RemoveDeadBindings(Store store,
Zhongxing Xu03fd7662010-03-17 03:35:08 +00001728 const StackFrameContext *LCtx,
Zhongxing Xuad0ef842010-02-05 05:34:29 +00001729 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001730 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001731{
Zhongxing Xuee770d42010-08-15 12:45:09 +00001732 RegionBindings B = GetRegionBindings(store);
Jordy Rosef7488ec2010-07-01 20:09:55 +00001733 RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, LCtx);
Ted Kremenekddce4a32010-03-10 16:32:56 +00001734 W.GenerateClusters();
Mike Stump11289f42009-09-09 15:08:12 +00001735
Ted Kremenekddce4a32010-03-10 16:32:56 +00001736 // Enqueue the region roots onto the worklist.
Ted Kremenekcc224242009-09-29 06:35:00 +00001737 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
Ted Kremenekddce4a32010-03-10 16:32:56 +00001738 E=RegionRoots.end(); I!=E; ++I)
1739 W.AddToWorkList(*I);
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001740
Ted Kremenekddce4a32010-03-10 16:32:56 +00001741 do W.RunWorkList(); while (W.UpdatePostponed());
Ted Kremenekba2e6c62010-03-10 07:20:03 +00001742
Ted Kremenek4533a552009-06-16 22:36:44 +00001743 // We have now scanned the store, marking reachable regions and symbols
1744 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00001745 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001746 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekddce4a32010-03-10 16:32:56 +00001747 const BindingKey &K = I.getKey();
1748
Ted Kremenek2ce82b22010-03-10 16:38:41 +00001749 // If the cluster has been visited, we know the region has been marked.
Ted Kremenekddce4a32010-03-10 16:32:56 +00001750 if (W.isVisited(K.getRegion()))
Ted Kremenek4533a552009-06-16 22:36:44 +00001751 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001752
Ted Kremenekddce4a32010-03-10 16:32:56 +00001753 // Remove the dead entry.
1754 B = Remove(B, K);
Mike Stump11289f42009-09-09 15:08:12 +00001755
Ted Kremenekddce4a32010-03-10 16:32:56 +00001756 // Mark all non-live symbols that this binding references as dead.
1757 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(K.getRegion()))
Ted Kremenek4533a552009-06-16 22:36:44 +00001758 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00001759
Ted Kremenek64efd0d2010-02-03 03:06:46 +00001760 SVal X = I.getData();
Ted Kremenekf106ab92009-08-02 05:00:15 +00001761 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1762 for (; SI != SE; ++SI)
1763 SymReaper.maybeDead(*SI);
1764 }
Zhongxing Xuee770d42010-08-15 12:45:09 +00001765
1766 return B.getRoot();
Ted Kremenek4533a552009-06-16 22:36:44 +00001767}
1768
Ted Kremenekddce4a32010-03-10 16:32:56 +00001769
Jordy Roseddec0922010-08-03 20:44:35 +00001770Store RegionStoreManager::EnterStackFrame(const GRState *state,
1771 const StackFrameContext *frame) {
Zhongxing Xudaa41762009-10-13 02:24:55 +00001772 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
Zhongxing Xudaa41762009-10-13 02:24:55 +00001773 FunctionDecl::param_const_iterator PI = FD->param_begin();
Zhongxing Xu7fcd8ac2010-02-05 05:06:13 +00001774 Store store = state->getStore();
Zhongxing Xu0eb69032010-03-16 13:14:16 +00001775
1776 if (CallExpr const *CE = dyn_cast<CallExpr>(frame->getCallSite())) {
1777 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1778
1779 // Copy the arg expression value to the arg variables.
1780 for (; AI != AE; ++AI, ++PI) {
1781 SVal ArgVal = state->getSVal(*AI);
1782 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1783 }
Ted Kremenekbd862712010-07-01 20:16:50 +00001784 } else if (const CXXConstructExpr *CE =
Zhongxing Xu0eb69032010-03-16 13:14:16 +00001785 dyn_cast<CXXConstructExpr>(frame->getCallSite())) {
Ted Kremenekbd862712010-07-01 20:16:50 +00001786 CXXConstructExpr::const_arg_iterator AI = CE->arg_begin(),
Zhongxing Xu0eb69032010-03-16 13:14:16 +00001787 AE = CE->arg_end();
1788
1789 // Copy the arg expression value to the arg variables.
1790 for (; AI != AE; ++AI, ++PI) {
1791 SVal ArgVal = state->getSVal(*AI);
1792 store = Bind(store, ValMgr.makeLoc(MRMgr.getVarRegion(*PI,frame)),ArgVal);
1793 }
1794 } else
Jordy Roseddec0922010-08-03 20:44:35 +00001795 llvm_unreachable("Unhandled call expression.");
Zhongxing Xudaa41762009-10-13 02:24:55 +00001796
Jordy Roseddec0922010-08-03 20:44:35 +00001797 return store;
Zhongxing Xudaa41762009-10-13 02:24:55 +00001798}
1799
Ted Kremenek4533a552009-06-16 22:36:44 +00001800//===----------------------------------------------------------------------===//
1801// Utility methods.
1802//===----------------------------------------------------------------------===//
1803
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001804void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00001805 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00001806 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00001807 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00001808
Ted Kremenek2c85f172009-08-06 04:50:20 +00001809 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001810 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00001811}