blob: 20549d1caa3ddf38ae6991122b09e3b14d2166e9 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
Zhongxing Xu0b143312009-08-21 13:25:15 +000018#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000019#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000020#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000024
25#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000027#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000028
29using namespace clang;
30
Ted Kremeneka5e81f12009-08-06 01:20:57 +000031#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000032
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000033//===----------------------------------------------------------------------===//
34// Representation of value bindings.
35//===----------------------------------------------------------------------===//
36
Zhongxing Xu13d50172009-10-11 08:08:02 +000037namespace {
38class BindingVal {
39public:
40 enum BindingKind { Direct, Default };
41private:
42 SVal Value;
43 BindingKind Kind;
44
45public:
46 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
47
48 bool isDefault() const { return Kind == Default; }
49
50 const SVal *getValue() const { return &Value; }
51
52 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
53
54 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
55
56 void Profile(llvm::FoldingSetNodeID& ID) const {
57 Value.Profile(ID);
58 ID.AddInteger(Kind);
59 }
60
61 inline bool operator==(const BindingVal& R) const {
62 return Value == R.Value && Kind == R.Kind;
63 }
64
65 inline bool operator!=(const BindingVal& R) const {
66 return !(*this == R);
67 }
68};
69}
70
71namespace llvm {
72static inline
73llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
74 if (V.isDefault())
75 os << "(default) ";
76 else
77 os << "(direct) ";
78 os << *V.getValue();
79 return os;
80}
81} // end llvm namespace
82
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +000083//===----------------------------------------------------------------------===//
84// Representation of binding keys.
85//===----------------------------------------------------------------------===//
86
87namespace {
88 class BindingKey : public std::pair<const MemRegion*, uint64_t> {
89public:
90 explicit BindingKey(const MemRegion *r)
91 : std::pair<const MemRegion*,uint64_t>(r,0) {}
92
93 const MemRegion *getRegion() const { return first; }
94 uint64_t getOffset() const { return second; }
95
96 void Profile(llvm::FoldingSetNodeID& ID) const {
97 ID.AddPointer(getRegion());
98 ID.AddInteger(getOffset());
99 }
100};
101} // end anonymous namespace
102
103namespace llvm {
104 static inline
105 llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingKey K) {
106 os << '(' << K.getRegion() << ',' << K.getOffset() << ')';
107 return os;
108 }
109} // end llvm namespace
110
111//===----------------------------------------------------------------------===//
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000112// Actual Store type.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000113//===----------------------------------------------------------------------===//
114
115typedef llvm::ImmutableMap<BindingKey, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000116
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000118// Fine-grained control of RegionStoreManager.
119//===----------------------------------------------------------------------===//
120
121namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000122struct minimal_features_tag {};
123struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000125class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000126 bool SupportsFields;
127 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenek9af46f52009-06-16 22:36:44 +0000129public:
130 RegionStoreFeatures(minimal_features_tag) :
131 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek9af46f52009-06-16 22:36:44 +0000133 RegionStoreFeatures(maximal_features_tag) :
134 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek9af46f52009-06-16 22:36:44 +0000136 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Ted Kremenek9af46f52009-06-16 22:36:44 +0000138 bool supportsFields() const { return SupportsFields; }
139 bool supportsRemaining() const { return SupportsRemaining; }
140};
141}
142
143//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000144// Region "Extents"
145//===----------------------------------------------------------------------===//
146//
147// MemRegions represent chunks of memory with a size (their "extent"). This
148// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000149//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000150namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000151static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000152namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000153 template<> struct GRStateTrait<RegionExtents>
154 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
155 static void* GDMIndex() { return &RegionExtentsIndex; }
156 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000157}
158
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000159//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000160// Utility functions.
161//===----------------------------------------------------------------------===//
162
163static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
164 if (ty->isAnyPointerType())
165 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000167 return ty->isIntegerType() && ty->isScalarType() &&
168 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
169}
170
171//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000172// Main RegionStore logic.
173//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000174
Zhongxing Xu17892752008-10-08 02:50:44 +0000175namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000177class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000178 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000179 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000180 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000181 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000182public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000183 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000184 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000185
186 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000187 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000188 return true;
189 }
190
191 I->second = F.Add(I->second, SubRegion);
192 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000193 }
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000195 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremenek59e8f112009-03-03 01:35:36 +0000197 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Ted Kremenek5dc27462009-03-03 02:51:43 +0000199 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000200 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000201
202 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000203 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenek59e8f112009-03-03 01:35:36 +0000205 llvm::ImmutableSet<const MemRegion*> S = I->second;
206 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
207 SI != SE; ++SI) {
208 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000209 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000210 }
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenek5dc27462009-03-03 02:51:43 +0000212 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000215 typedef SetTy::iterator iterator;
216
217 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
218 Map::iterator I = M.find(R);
219 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
220 return std::make_pair(S.begin(), S.end());
221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000223
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000224class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000225 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000226 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000227
228 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
229 SMCache SC;
230
Zhongxing Xu17892752008-10-08 02:50:44 +0000231public:
Mike Stump1eb44332009-09-09 15:08:12 +0000232 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000233 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000234 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000235 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000236
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000237 virtual ~RegionStoreManager() {
238 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
239 delete (*I).second;
240 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000241
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000242 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000243
Zhongxing Xu13d50172009-10-11 08:08:02 +0000244 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Zhongxing Xu13d50172009-10-11 08:08:02 +0000246 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
247 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000248 /// getDefaultBinding - Returns an SVal* representing an optional default
249 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000250 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000251
252 /// setImplicitDefaultValue - Set the default binding for the provided
253 /// MemRegion to the value implicitly defined for compound literals when
254 /// the value is not specified.
255 const GRState *setImplicitDefaultValue(const GRState *state,
256 const MemRegion *R,
257 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000259 /// getLValueString - Returns an SVal representing the lvalue of a
260 /// StringLiteral. Within RegionStore a StringLiteral has an
261 /// associated StringRegion, and the lvalue of a StringLiteral is
262 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000263 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000264
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000265 /// getLValueCompoundLiteral - Returns an SVal representing the
266 /// lvalue of a compound literal. Within RegionStore a compound
267 /// literal has an associated region, and the lvalue of the
268 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000269 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000270
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000271 /// getLValueVar - Returns an SVal that represents the lvalue of a
272 /// variable. Within RegionStore a variable has an associated
273 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000274 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000276 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000277
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000278 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000280 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000281
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000282 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000283
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000284
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000285 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
286 /// type. 'Array' represents the lvalue of the array being decayed
287 /// to a pointer, and the returned SVal represents the decayed
288 /// version of that lvalue (i.e., a pointer to the first element of
289 /// the array). This is called by GRExprEngine when evaluating
290 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000291 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000292
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000293 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000294 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000295
Mike Stump1eb44332009-09-09 15:08:12 +0000296 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000297 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000298 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 //===-------------------------------------------------------------------===//
301 // Binding values to regions.
302 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000303
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000304 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000305 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-12-03 03:27:11 +0000306 InvalidatedSymbols *IS) {
307 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
308 }
309
310 const GRState *InvalidateRegions(const GRState *state,
311 const MemRegion * const *Begin,
312 const MemRegion * const *End,
313 const Expr *E, unsigned Count,
314 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000316private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000317 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000318 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000320 RegionBindings Add(RegionBindings B, BindingKey K, BindingVal V);
321 RegionBindings Add(RegionBindings B, const MemRegion *R, BindingVal V);
322
323 const BindingVal *Lookup(RegionBindings B, BindingKey K);
324 const BindingVal *Lookup(RegionBindings B, const MemRegion *R);
325
326 RegionBindings Remove(RegionBindings B, BindingKey K);
327 RegionBindings Remove(RegionBindings B, const MemRegion *R);
328 Store Remove(Store store, BindingKey K);
329
Mike Stump1eb44332009-09-09 15:08:12 +0000330public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000331 const GRState *Bind(const GRState *state, Loc LV, SVal V);
332
333 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +0000334 const CompoundLiteralExpr* CL,
335 const LocationContext *LC,
336 SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000338 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
339 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000340
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000341 const GRState *BindDeclWithNoInit(const GRState *state,
342 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000343 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000344 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000345
Ted Kremenek67f28532009-06-17 22:02:04 +0000346 /// BindStruct - Bind a compound value to a structure.
347 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
351 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000352 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000353
Ted Kremenek67f28532009-06-17 22:02:04 +0000354 Store Remove(Store store, Loc LV);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000355
Ted Kremenek67f28532009-06-17 22:02:04 +0000356
357 //===------------------------------------------------------------------===//
358 // Loading values from regions.
359 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 /// The high level logic for this method is this:
362 /// Retrieve (L)
363 /// if L has binding
364 /// return L's binding
365 /// else if L is in killset
366 /// return unknown
367 /// else
368 /// if L is on stack or heap
369 /// return undefined
370 /// else
371 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000372 SValuator::CastResult Retrieve(const GRState *state, Loc L,
373 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000374
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000375 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000376
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000377 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000379 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremenek9031dd72009-07-21 00:12:07 +0000381 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek25c54572009-07-20 22:58:02 +0000383 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000385 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
386 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Ted Kremenek67f28532009-06-17 22:02:04 +0000388 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000389 /// struct copy:
390 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000391 /// x = y;
392 /// y's value is retrieved by this method.
393 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Ted Kremenek67f28532009-06-17 22:02:04 +0000395 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000397 /// Get the state and region whose binding this region R corresponds to.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000398 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000399 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000401 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
402 const GRState *state,
403 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000404
Ted Kremenek0954cde2009-09-24 04:11:44 +0000405 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
406 QualType T);
407
Ted Kremenek67f28532009-06-17 22:02:04 +0000408 //===------------------------------------------------------------------===//
409 // State pruning.
410 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Ted Kremenek67f28532009-06-17 22:02:04 +0000412 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
413 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000414 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000415 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
416
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000417 const GRState *EnterStackFrame(const GRState *state,
418 const StackFrameContext *frame);
419
Ted Kremenek67f28532009-06-17 22:02:04 +0000420 //===------------------------------------------------------------------===//
421 // Region "extents".
422 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Ted Kremenek67f28532009-06-17 22:02:04 +0000424 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000425 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
426 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000427
428 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000429 // Utility methods.
430 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Ted Kremenek451ac092009-08-06 04:50:20 +0000432 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000433 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000434 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000435
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000436 void print(Store store, llvm::raw_ostream& Out, const char* nl,
437 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000438
439 void iterBindings(Store store, BindingsHandler& f) {
440 // FIXME: Implement.
441 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000442
Ted Kremenek67f28532009-06-17 22:02:04 +0000443 // FIXME: Remove.
444 BasicValueFactory& getBasicVals() {
445 return StateMgr.getBasicVals();
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Ted Kremenek67f28532009-06-17 22:02:04 +0000448 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000449 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000450};
451
452} // end anonymous namespace
453
Ted Kremenek9af46f52009-06-16 22:36:44 +0000454//===----------------------------------------------------------------------===//
455// RegionStore creation.
456//===----------------------------------------------------------------------===//
457
458StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
459 RegionStoreFeatures F = maximal_features_tag();
460 return new RegionStoreManager(StMgr, F);
461}
462
463StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
464 RegionStoreFeatures F = minimal_features_tag();
465 F.enableFields(true);
466 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000467}
468
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000469void
470RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000471 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000472 const MemRegion *superR = R->getSuperRegion();
473 if (add(superR, R))
474 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000475 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000476}
477
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000478RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000479RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
480 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000481 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000483 llvm::SmallVector<const SubRegion*, 10> WL;
484
Ted Kremenek451ac092009-08-06 04:50:20 +0000485 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000486 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey().getRegion()))
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000487 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Mike Stump1eb44332009-09-09 15:08:12 +0000489 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000490 // don't have direct bindings but are super regions of those that do.
491 while (!WL.empty()) {
492 const SubRegion *R = WL.back();
493 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000494 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000495 }
496
Ted Kremenek14453bf2009-03-03 19:02:42 +0000497 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000498}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000499
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000500SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000501 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000502}
503
Ted Kremenek9af46f52009-06-16 22:36:44 +0000504//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000505// Binding invalidation.
506//===----------------------------------------------------------------------===//
507
Zhongxing Xu13d50172009-10-11 08:08:02 +0000508void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
509 const MemRegion *R,
510 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000511 RegionStoreSubRegionMap::iterator I, E;
512
513 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000514 RemoveSubRegionBindings(B, *I, M);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000515
516 B = Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000517}
518
Ted Kremenek81a95832009-12-03 03:27:11 +0000519const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
520 const MemRegion * const *I,
521 const MemRegion * const *E,
522 const Expr *Ex,
523 unsigned Count,
524 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000525 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek87806792009-09-27 20:45:21 +0000527 // Get the mapping of regions -> subregions.
528 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000529 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000530
531 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000532
Ted Kremenek87806792009-09-27 20:45:21 +0000533 llvm::DenseMap<const MemRegion *, unsigned> Visited;
534 llvm::SmallVector<const MemRegion *, 10> WorkList;
Ted Kremenek81a95832009-12-03 03:27:11 +0000535
536 for ( ; I != E; ++I) {
537 // Strip away casts.
538 WorkList.push_back((*I)->StripCasts());
539 }
Ted Kremenek87806792009-09-27 20:45:21 +0000540
541 while (!WorkList.empty()) {
Ted Kremenek81a95832009-12-03 03:27:11 +0000542 const MemRegion *R = WorkList.back();
Ted Kremenek87806792009-09-27 20:45:21 +0000543 WorkList.pop_back();
544
545 // Have we visited this region before?
546 unsigned &visited = Visited[R];
547 if (visited)
548 continue;
549 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek87806792009-09-27 20:45:21 +0000551 // Add subregions to work list.
552 RegionStoreSubRegionMap::iterator I, E;
553 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
554 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000555
556 // Get the old binding. Is it a region? If so, add it to the worklist.
557 if (Optional<SVal> V = getDirectBinding(B, R)) {
558 if (const MemRegion *RV = V->getAsRegion())
559 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000560
561 // A symbol? Mark it touched by the invalidation.
562 if (IS) {
563 if (SymbolRef Sym = V->getAsSymbol())
564 IS->insert(Sym);
565 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000566 }
567
Ted Kremenek473e1672009-10-16 00:30:49 +0000568 // Symbolic region? Mark that symbol touched by the invalidation.
569 if (IS) {
570 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
571 IS->insert(SR->getSymbol());
572 }
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000573
574 // BlockDataRegion? If so, invalidate captured variables that are passed
575 // by reference.
576 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
577 for (BlockDataRegion::referenced_vars_iterator
578 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
579 I != E; ++I) {
580 const VarRegion *VR = *I;
581 if (VR->getDecl()->getAttr<BlocksAttr>())
582 WorkList.push_back(VR);
583 }
584 continue;
585 }
Ted Kremenek473e1672009-10-16 00:30:49 +0000586
587 // Handle the region itself.
Ted Kremenekc410d4d2009-12-16 23:53:37 +0000588 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000589 // Invalidate the region by setting its default value to
590 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000591 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000592 Count);
593 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000594 continue;
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Ted Kremenek87806792009-09-27 20:45:21 +0000597 if (!R->isBoundable())
598 continue;
599
600 const TypedRegion *TR = cast<TypedRegion>(R);
601 QualType T = TR->getValueType(Ctx);
602
603 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000604 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
605
Zhongxing Xu13d50172009-10-11 08:08:02 +0000606 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000607 if (!RD)
608 continue;
609
Zhongxing Xu13d50172009-10-11 08:08:02 +0000610 // Invalidate the region by setting its default value to
611 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000612 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
613 Count);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000614 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000615 continue;
616 }
617
618 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
619 // Set the default value of the array to conjured symbol.
620 DefinedOrUnknownSVal V =
621 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000622 B = Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000623 continue;
624 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000625
626 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
627 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000628 // For fields and elements whose super region has also been invalidated,
629 // only remove the old binding. The super region will get set with a
630 // default value from which we can lazily derive a new symbolic value.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000631 B = Remove(B, R);
Ted Kremeneka5971b32009-09-29 03:34:03 +0000632 continue;
633 }
Ted Kremenek87806792009-09-27 20:45:21 +0000634
Ted Kremenek389c44c2009-09-29 03:12:50 +0000635 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000636 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
637 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000638 B = Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000639 }
640
Ted Kremenek87806792009-09-27 20:45:21 +0000641 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000642 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000643}
644
645//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000646// getLValueXXX methods.
647//===----------------------------------------------------------------------===//
648
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000649/// getLValueString - Returns an SVal representing the lvalue of a
650/// StringLiteral. Within RegionStore a StringLiteral has an
651/// associated StringRegion, and the lvalue of a StringLiteral is the
652/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000653SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000654 return loc::MemRegionVal(MRMgr.getStringRegion(S));
655}
656
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000657/// getLValueVar - Returns an SVal that represents the lvalue of a
658/// variable. Within RegionStore a variable has an associated
659/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000660SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000661 const LocationContext *LC) {
662 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000663}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000664
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000665SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
666 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000667}
668
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000669SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
670 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000671}
672
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000673SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000674 if (Base.isUnknownOrUndef())
675 return Base;
676
677 Loc BaseL = cast<Loc>(Base);
678 const MemRegion* BaseR = 0;
679
680 switch (BaseL.getSubKind()) {
681 case loc::MemRegionKind:
682 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
683 break;
684
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000685 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000686 // These are anormal cases. Flag an undefined value.
687 return UndefinedVal();
688
689 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000690 // While these seem funny, this can happen through casts.
691 // FIXME: What we should return is the field offset. For example,
692 // add the field offset to the integer value. That way funny things
693 // like this work properly: &(((struct foo *) 0xa)->f)
694 return Base;
695
696 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000697 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000698 return Base;
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000701 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
702 // of FieldDecl.
703 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
704 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000705
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000706 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000707}
708
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000709SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
710 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000711
Ted Kremenekde7ec632009-03-09 22:44:49 +0000712 // If the base is an unknown or undefined value, just return it back.
713 // FIXME: For absolute pointer addresses, we just return that value back as
714 // well, although in reality we should return the offset added to that
715 // value.
716 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000717 return Base;
718
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000719 // Only handle integer offsets... for now.
720 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000721 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000722
Zhongxing Xuce760782009-05-09 13:20:07 +0000723 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000724
725 // Pointer of any type can be cast and used as array base.
726 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Ted Kremenek46537392009-07-16 01:33:37 +0000728 // Convert the offset to the appropriate size and signedness.
729 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000731 if (!ElemR) {
732 //
733 // If the base region is not an ElementRegion, create one.
734 // This can happen in the following example:
735 //
736 // char *p = __builtin_alloc(10);
737 // p[1] = 8;
738 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000739 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000740 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000741 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000742 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000745 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000747 if (!isa<nonloc::ConcreteInt>(BaseIdx))
748 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000750 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
751 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
752 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenek46537392009-07-16 01:33:37 +0000754 // Compute the new index.
755 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek46537392009-07-16 01:33:37 +0000757 // Construct the new ElementRegion.
758 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000759 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000760 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000761}
762
Ted Kremenek9af46f52009-06-16 22:36:44 +0000763//===----------------------------------------------------------------------===//
764// Extents for regions.
765//===----------------------------------------------------------------------===//
766
Zhongxing Xue884ff82009-11-12 02:48:32 +0000767DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
768 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000770 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000771 case MemRegion::CXXThisRegionKind:
772 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000773 case MemRegion::GenericMemSpaceRegionKind:
774 case MemRegion::StackLocalsSpaceRegionKind:
775 case MemRegion::StackArgumentsSpaceRegionKind:
776 case MemRegion::HeapSpaceRegionKind:
777 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000778 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000779 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000780 return UnknownVal();
781
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000782 case MemRegion::FunctionTextRegionKind:
783 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000784 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000785 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000786 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000787
788 // Not yet handled.
789 case MemRegion::AllocaRegionKind:
790 case MemRegion::CompoundLiteralRegionKind:
791 case MemRegion::ElementRegionKind:
792 case MemRegion::FieldRegionKind:
793 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000794 case MemRegion::SymbolicRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000795 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000796 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000798 case MemRegion::StringRegionKind: {
799 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000800 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000801 // operations with signed indices.
802 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000805 case MemRegion::VarRegionKind: {
806 const VarRegion* VR = cast<VarRegion>(R);
807 // Get the type of the variable.
808 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000810 // FIXME: Handle variable-length arrays.
811 if (isa<VariableArrayType>(T))
812 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000814 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
815 // return the size as signed integer.
816 return ValMgr.makeIntVal(CAT->getSize(), false);
817 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000818
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000819 // Clients can use ordinary variables as if they were arrays. These
820 // essentially are arrays of size 1.
821 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000822 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000825 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000826 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000827}
828
Ted Kremenek67f28532009-06-17 22:02:04 +0000829const GRState *RegionStoreManager::setExtent(const GRState *state,
830 const MemRegion *region,
831 SVal extent) {
832 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000833}
834
835//===----------------------------------------------------------------------===//
836// Location and region casting.
837//===----------------------------------------------------------------------===//
838
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000839/// ArrayToPointer - Emulates the "decay" of an array to a pointer
840/// type. 'Array' represents the lvalue of the array being decayed
841/// to a pointer, and the returned SVal represents the decayed
842/// version of that lvalue (i.e., a pointer to the first element of
843/// the array). This is called by GRExprEngine when evaluating casts
844/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000845SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000846 if (!isa<loc::MemRegionVal>(Array))
847 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenekabb042f2008-12-13 19:24:37 +0000849 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
850 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000852 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000853 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000855 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000856 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000857 ArrayType *AT = cast<ArrayType>(T);
858 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Ted Kremenek75185b52009-07-16 00:00:11 +0000860 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000861 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
862 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000863}
864
Ted Kremenek9af46f52009-06-16 22:36:44 +0000865//===----------------------------------------------------------------------===//
866// Pointer arithmetic.
867//===----------------------------------------------------------------------===//
868
Mike Stump1eb44332009-09-09 15:08:12 +0000869SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000870 BinaryOperator::Opcode Op, Loc L, NonLoc R,
871 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000872 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000873 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000874 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000875
Zhongxing Xua1718c72009-04-03 07:33:13 +0000876 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000877 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000878
Ted Kremenek3bccf082009-07-11 00:58:27 +0000879 switch (MR->getKind()) {
880 case MemRegion::SymbolicRegionKind: {
881 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000882 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000883 QualType T = Sym->getType(getContext());
884 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000886 if (const PointerType *PT = T->getAs<PointerType>())
887 EleTy = PT->getPointeeType();
888 else
John McCall183700f2009-09-21 23:43:11 +0000889 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Ted Kremenek3bccf082009-07-11 00:58:27 +0000891 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
892 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000893 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000894 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000895 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000896 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000897 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000898 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000899 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
900 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000901 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000902 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000903
Ted Kremenek3bccf082009-07-11 00:58:27 +0000904 case MemRegion::ElementRegionKind: {
905 ER = cast<ElementRegion>(MR);
906 break;
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Ted Kremenek3bccf082009-07-11 00:58:27 +0000909 // Not yet handled.
910 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000911 case MemRegion::StringRegionKind: {
912
913 }
914 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000915 case MemRegion::CompoundLiteralRegionKind:
916 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000917 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000918 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000919 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000921 case MemRegion::FunctionTextRegionKind:
922 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000923 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000924 // Technically this can happen if people do funny things with casts.
925 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremenekde0d2632010-01-05 02:18:06 +0000927 case MemRegion::CXXThisRegionKind:
928 assert(0 &&
929 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000930 case MemRegion::GenericMemSpaceRegionKind:
931 case MemRegion::StackLocalsSpaceRegionKind:
932 case MemRegion::StackArgumentsSpaceRegionKind:
933 case MemRegion::HeapSpaceRegionKind:
934 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000935 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000936 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
937 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000938 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000939
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000940 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000941 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000942
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000943 // For now, only support:
944 // (a) concrete integer indices that can easily be resolved
945 // (b) 0 + symbolic index
946 if (Base) {
947 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
948 // FIXME: Should use SValuator here.
949 SVal NewIdx =
950 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000951 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000952 const MemRegion* NewER =
953 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
954 ER->getSuperRegion(), getContext());
955 return ValMgr.makeLoc(NewER);
956 }
957 if (0 == Base->getValue()) {
958 const MemRegion* NewER =
959 MRMgr.getElementRegion(ER->getElementType(), R,
960 ER->getSuperRegion(), getContext());
961 return ValMgr.makeLoc(NewER);
962 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000963 }
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Ted Kremenek5dc27462009-03-03 02:51:43 +0000965 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000966}
967
Ted Kremenek9af46f52009-06-16 22:36:44 +0000968//===----------------------------------------------------------------------===//
969// Loading values from regions.
970//===----------------------------------------------------------------------===//
971
Zhongxing Xu13d50172009-10-11 08:08:02 +0000972Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
973 const MemRegion *R) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000974 if (const BindingVal *BV = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +0000975 return Optional<SVal>::create(BV->getDirectValue());
976
977 return Optional<SVal>();
978}
979
980Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000981 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000983 if (R->isBoundable())
984 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
985 if (TR->getValueType(getContext())->isUnionType())
986 return UnknownVal();
987
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000988 if (const BindingVal *V = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +0000989 return Optional<SVal>::create(V->getDefaultValue());
990
991 return Optional<SVal>();
992}
993
994Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
995 const MemRegion *R) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +0000996 if (const BindingVal *BV = Lookup(B, R))
Zhongxing Xu13d50172009-10-11 08:08:02 +0000997 return Optional<SVal>::create(BV->getValue());
998
999 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001000}
1001
Ted Kremeneka6275a52009-07-15 02:31:43 +00001002static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
1003 RTy = Ctx.getCanonicalType(RTy);
1004 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Ted Kremeneka6275a52009-07-15 02:31:43 +00001006 if (RTy == UsedTy)
1007 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
1009
Ted Kremenek25c54572009-07-20 22:58:02 +00001010 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +00001011 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +00001012 // represents a reinterpretation.
1013 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001014 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +00001015 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +00001016
1017 return PUsedTy && PRTy &&
1018 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +00001019 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +00001020 }
1021
1022 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +00001023}
1024
Ted Kremenek0954cde2009-09-24 04:11:44 +00001025const ElementRegion *
1026RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
1027 ASTContext &Ctx = getContext();
1028 SVal idx = ValMgr.makeZeroArrayIndex();
1029 assert(!T.isNull());
1030 return MRMgr.getElementRegion(T, idx, SR, Ctx);
1031}
1032
1033
1034
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001035SValuator::CastResult
1036RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001037
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001038 assert(!isa<UnknownVal>(L) && "location unknown");
1039 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +00001040
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001041 // FIXME: Is this even possible? Shouldn't this be treated as a null
1042 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001043 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001044 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek28172a22009-12-15 23:23:27 +00001045
Ted Kremenek67f28532009-06-17 22:02:04 +00001046 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001047
Zhongxing Xu91844122009-05-20 09:18:48 +00001048 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001049 // Example:
1050 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001051 // char* p = alloca();
1052 // read(p);
1053 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001054 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001055 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremenek0954cde2009-09-24 04:11:44 +00001057 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1058 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Ted Kremenek968f0a62009-08-03 21:41:46 +00001060 if (isa<CodeTextRegion>(MR))
1061 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001063 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1064 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001065 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001066 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001067
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001068 // FIXME: We should eventually handle funny addressing. e.g.:
1069 //
1070 // int x = ...;
1071 // int *p = &x;
1072 // char *q = (char*) p;
1073 // char c = *q; // returns the first byte of 'x'.
1074 //
1075 // Such funny addressing will occur due to layering of regions.
1076
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001077#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001078 ASTContext &Ctx = getContext();
1079 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001080 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1081 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001082 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001083 assert(Ctx.getCanonicalType(RTy) ==
1084 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001085 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001086#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001087
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001088 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001089 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001091 // FIXME: Handle unions.
1092 if (RTy->isUnionType())
1093 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001094
1095 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001096 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001097
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001098 // FIXME: handle Vector types.
1099 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001100 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001101
1102 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001103 return SValuator::CastResult(state,
1104 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001105
1106 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001107 return SValuator::CastResult(state,
1108 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Ted Kremenek25c54572009-07-20 22:58:02 +00001110 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001111 return SValuator::CastResult(state,
1112 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremenek9031dd72009-07-21 00:12:07 +00001114 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001115 return SValuator::CastResult(state,
1116 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001117
Ted Kremenek451ac092009-08-06 04:50:20 +00001118 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001119 const BindingVal *V = Lookup(B, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001120
1121 // Check if the region has a binding.
1122 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001123 if (SVal const *SV = V->getValue())
1124 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001125
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001126 // The location does not have a bound value. This means that it has
1127 // the value it had upon its creation and/or entry to the analyzed
1128 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001129 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001130 // All stack variables are considered to have undefined values
1131 // upon creation. All heap allocated blocks are considered to
1132 // have undefined values as well unless they are explicitly bound
1133 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001134 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001135 }
1136
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001137 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001138 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001139}
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001141std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001142RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001143 if (Optional<SVal> OV = getDirectBinding(B, R))
1144 if (const nonloc::LazyCompoundVal *V =
1145 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1146 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001148 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1149 const std::pair<const GRState *, const MemRegion *> &X =
1150 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001151
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001152 if (X.first)
1153 return std::make_pair(X.first,
1154 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001155 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001156 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1157 const std::pair<const GRState *, const MemRegion *> &X =
1158 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001160 if (X.first)
1161 return std::make_pair(X.first,
1162 MRMgr.getFieldRegionWithSuper(FR, X.second));
1163 }
1164
1165 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1166}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001167
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001168SVal RegionStoreManager::RetrieveElement(const GRState* state,
1169 const ElementRegion* R) {
1170 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001171 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001172 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001173 return *V;
1174
Ted Kremenek921109a2009-07-01 23:19:52 +00001175 const MemRegion* superR = R->getSuperRegion();
1176
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001177 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001178 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001179 // FIXME: Handle loads from strings where the literal is treated as
1180 // an integer, e.g., *((unsigned int*)"hello")
1181 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001182 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001183 if (T != Ctx.getCanonicalType(R->getElementType()))
1184 return UnknownVal();
1185
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001186 const StringLiteral *Str = StrR->getStringLiteral();
1187 SVal Idx = R->getIndex();
1188 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1189 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001190 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001191 if (i > byteLength) {
1192 // Buffer overflow checking in GRExprEngine should handle this case,
1193 // but we shouldn't rely on it to not overflow here if that checking
1194 // is disabled.
1195 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001196 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001197 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001198 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001199 }
1200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001202 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001203 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001204 if (SymbolRef parentSym = V->getAsSymbol())
1205 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001206
1207 if (V->isUnknownOrUndef())
1208 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001209
1210 // Handle LazyCompoundVals for the immediate super region. Other cases
1211 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001212 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001213 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001215 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1216 return RetrieveElement(LCV->getState(), R);
1217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremeneka6275a52009-07-15 02:31:43 +00001219 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001220 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001221 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001222
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001223 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001224}
1225
Mike Stump1eb44332009-09-09 15:08:12 +00001226SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001227 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001228
1229 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001230 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001231 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001232 return *V;
1233
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001234 QualType Ty = R->getValueType(getContext());
1235 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1236}
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001238SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1239 const TypedRegion *R,
1240 QualType Ty,
1241 const MemRegion *superR) {
1242
Mike Stump1eb44332009-09-09 15:08:12 +00001243 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001244 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001246 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001248 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001249 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001250 if (SymbolRef parentSym = D->getAsSymbol())
1251 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001253 if (D->isZeroConstant())
1254 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001256 if (D->isUnknown())
1257 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001259 assert(0 && "Unknown default value");
1260 }
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001262 // If our super region is a field or element itself, walk up the region
1263 // hierarchy to see if there is a default value installed in an ancestor.
1264 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1265 superR = cast<SubRegion>(superR)->getSuperRegion();
1266 continue;
1267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001269 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001270 }
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001272 // Lazy binding?
1273 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001274 const MemRegion *lazyBindingRegion = NULL;
1275 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001277 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001278 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001280 if (isa<ElementRegion>(R))
1281 return RetrieveElement(lazyBindingState,
1282 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001284 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001285 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001286 }
1287
Ted Kremenekde0d2632010-01-05 02:18:06 +00001288 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001289 if (isa<ElementRegion>(R)) {
1290 // Currently we don't reason specially about Clang-style vectors. Check
1291 // if superR is a vector and if so return Unknown.
1292 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1293 if (typedSuperR->getValueType(getContext())->isVectorType())
1294 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001295 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001296 }
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001298 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001299 }
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001301 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001302 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001303}
Mike Stump1eb44332009-09-09 15:08:12 +00001304
1305SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001306 const ObjCIvarRegion* R) {
1307
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001308 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001309 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001310
Zhongxing Xu13d50172009-10-11 08:08:02 +00001311 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001312 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001314 const MemRegion *superR = R->getSuperRegion();
1315
Ted Kremenekab22ee92009-10-20 01:20:57 +00001316 // Check if the super region has a default binding.
1317 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001318 if (SymbolRef parentSym = V->getAsSymbol())
1319 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001321 // Other cases: give up.
1322 return UnknownVal();
1323 }
Mike Stump1eb44332009-09-09 15:08:12 +00001324
Ted Kremenek25c54572009-07-20 22:58:02 +00001325 return RetrieveLazySymbol(state, R);
1326}
1327
Ted Kremenek9031dd72009-07-21 00:12:07 +00001328SVal RegionStoreManager::RetrieveVar(const GRState *state,
1329 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Ted Kremenek9031dd72009-07-21 00:12:07 +00001331 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001332 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Zhongxing Xu13d50172009-10-11 08:08:02 +00001334 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001335 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Ted Kremenek9031dd72009-07-21 00:12:07 +00001337 // Lazily derive a value for the VarRegion.
1338 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001340 if (R->hasGlobalsOrParametersStorage() ||
1341 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek28172a22009-12-15 23:23:27 +00001342 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Ted Kremenek9031dd72009-07-21 00:12:07 +00001344 return UndefinedVal();
1345}
1346
Mike Stump1eb44332009-09-09 15:08:12 +00001347SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001348 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001349
Ted Kremenek25c54572009-07-20 22:58:02 +00001350 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001351
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001352 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001353 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001354}
1355
Mike Stump1eb44332009-09-09 15:08:12 +00001356SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1357 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001358 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001359 assert(T->isStructureType());
1360
Zhongxing Xub7507d12009-06-11 07:27:30 +00001361 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001362 RecordDecl* RD = RT->getDecl();
1363 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001364 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001365#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001366 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1367
Ted Kremenek67f28532009-06-17 22:02:04 +00001368 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1369 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001370 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001371
Douglas Gregore267ff32008-12-11 20:41:00 +00001372 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1373 FieldEnd = Fields.rend();
1374 Field != FieldEnd; ++Field) {
1375 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001376 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001377 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001378 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1379 }
1380
Zhongxing Xud91ee272009-06-23 09:02:15 +00001381 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001382#else
1383 return ValMgr.makeLazyCompoundVal(state, R);
1384#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001385}
1386
Ted Kremenek67f28532009-06-17 22:02:04 +00001387SVal RegionStoreManager::RetrieveArray(const GRState *state,
1388 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001389#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001390 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001391 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1392
1393 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001394 uint64_t size = CAT->getSize().getZExtValue();
1395 for (uint64_t i = 0; i < size; ++i) {
1396 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001397 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001398 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001399 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001400 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001401 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1402 }
1403
Zhongxing Xud91ee272009-06-23 09:02:15 +00001404 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001405#else
1406 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1407 return ValMgr.makeLazyCompoundVal(state, R);
1408#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001409}
1410
Ted Kremenek9af46f52009-06-16 22:36:44 +00001411//===----------------------------------------------------------------------===//
1412// Binding values to regions.
1413//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001414
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001415Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001416 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Ted Kremenek0964a062009-01-21 06:57:53 +00001418 if (isa<loc::MemRegionVal>(L))
1419 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001421 if (R)
1422 return Remove(store, BindingKey(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Ted Kremenek0964a062009-01-21 06:57:53 +00001424 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001425}
1426
Ted Kremenek67f28532009-06-17 22:02:04 +00001427const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001428 if (isa<loc::ConcreteInt>(L))
1429 return state;
1430
Ted Kremenek9af46f52009-06-16 22:36:44 +00001431 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001432 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Ted Kremenek9af46f52009-06-16 22:36:44 +00001434 // Check if the region is a struct region.
1435 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1436 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001437 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001439 // Special case: the current region represents a cast and it and the super
1440 // region both have pointer types or intptr_t types. If so, perform the
1441 // bind to the super region.
1442 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001443 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001444 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1445 if (ER->getIndex().isZeroConstant()) {
1446 if (const TypedRegion *superR =
1447 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1448 ASTContext &Ctx = getContext();
1449 QualType superTy = superR->getValueType(Ctx);
1450 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001451
1452 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001453 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001454 SValuator::CastResult cr =
1455 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001456 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1457 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001458 // For now, just invalidate the fields of the struct/union/class.
1459 // FIXME: Precisely handle the fields of the record.
1460 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001461 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001462 }
1463 }
1464 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001465 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1466 // Binding directly to a symbolic region should be treated as binding
1467 // to element 0.
1468 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek852274d2009-12-16 03:18:58 +00001469
1470 // FIXME: Is this the right way to handle symbols that are references?
1471 if (const PointerType *PT = T->getAs<PointerType>())
1472 T = PT->getPointeeType();
1473 else
1474 T = T->getAs<ReferenceType>()->getPointeeType();
1475
Ted Kremenek0954cde2009-09-24 04:11:44 +00001476 R = GetElementZeroRegion(SR, T);
1477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001479 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001480 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001481 return state->makeWithStore(Add(B, R,
1482 BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001483}
1484
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001485const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001486 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001487 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001488
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001489 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001490
Ted Kremenek0964a062009-01-21 06:57:53 +00001491 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001492 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001493 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001494 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001495
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001496 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001497}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001498
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001499// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001500const GRState *
1501RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001502 const CompoundLiteralExpr *CL,
1503 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001504 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001505 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1506 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001507}
1508
Ted Kremenek027e2662009-11-19 20:20:24 +00001509const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1510 const MemRegion *R,
1511 QualType T) {
1512 Store store = state->getStore();
1513 RegionBindings B = GetRegionBindings(store);
1514 SVal V;
1515
1516 if (Loc::IsLocType(T))
1517 V = ValMgr.makeNull();
1518 else if (T->isIntegerType())
1519 V = ValMgr.makeZeroVal(T);
1520 else if (T->isStructureType() || T->isArrayType()) {
1521 // Set the default value to a zero constant when it is a structure
1522 // or array. The type doesn't really matter.
1523 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1524 }
1525 else {
1526 return state;
1527 }
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001528
1529 return state->makeWithStore(Add(B, R,
1530 BindingVal(V, BindingVal::Default)).getRoot());
Ted Kremenek027e2662009-11-19 20:20:24 +00001531}
1532
Ted Kremenek67f28532009-06-17 22:02:04 +00001533const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001534 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001535 SVal Init) {
1536
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001537 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001538 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001539 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001540
Ted Kremenek46537392009-07-16 01:33:37 +00001541 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001542
1543 // Check if the init expr is a StringLiteral.
1544 if (isa<loc::MemRegionVal>(Init)) {
1545 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1546 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1547 const char* str = S->getStrData();
1548 unsigned len = S->getByteLength();
1549 unsigned j = 0;
1550
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001551 // Copy bytes from the string literal into the target array. Trailing bytes
1552 // in the array that are not covered by the string literal are initialized
1553 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001554 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001555 if (j >= len)
1556 break;
1557
Ted Kremenek46537392009-07-16 01:33:37 +00001558 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001559 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1560 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001561
Zhongxing Xud91ee272009-06-23 09:02:15 +00001562 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001563 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001564 }
1565
Ted Kremenek67f28532009-06-17 22:02:04 +00001566 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001567 }
1568
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001569 // Handle lazy compound values.
1570 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1571 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
1573 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001574
1575 if (Init.isUnknown())
1576 return setImplicitDefaultValue(state, R, ElementTy);
1577
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001578 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001579 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001580 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremenek46537392009-07-16 01:33:37 +00001582 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001583 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001584 if (VI == VE)
1585 break;
1586
Ted Kremenek46537392009-07-16 01:33:37 +00001587 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001588 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001589
1590 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001591 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001592 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001593 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001594 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001595 }
1596
Ted Kremenek027e2662009-11-19 20:20:24 +00001597 // If the init list is shorter than the array length, set the
1598 // array default value.
1599 if (i < size)
1600 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001601
Ted Kremenek67f28532009-06-17 22:02:04 +00001602 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001603}
1604
Ted Kremenek67f28532009-06-17 22:02:04 +00001605const GRState *
1606RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1607 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Ted Kremenek67f28532009-06-17 22:02:04 +00001609 if (!Features.supportsFields())
1610 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001612 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001613 assert(T->isStructureType());
1614
Ted Kremenek6217b802009-07-29 21:53:49 +00001615 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001616 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001617
1618 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001619 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001620
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001621 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001622 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001623 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Ted Kremenek67f28532009-06-17 22:02:04 +00001625 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1626 // Ignore them and kill the field values.
1627 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001628 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001629
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001630 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001631 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001632
1633 RecordDecl::field_iterator FI, FE;
1634
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001635 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001636
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001637 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001638 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001639
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001640 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001641 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001642
Ted Kremenekcf549592009-09-22 21:19:14 +00001643 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001644 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001645 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001646 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001647 else
1648 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001649 }
1650
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001651 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001652 if (FI != FE) {
1653 Store store = state->getStore();
1654 RegionBindings B = GetRegionBindings(store);
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001655 B = Add(B, R, BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001656 state = state->makeWithStore(B.getRoot());
1657 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001658
Ted Kremenek67f28532009-06-17 22:02:04 +00001659 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001660}
1661
Zhongxing Xu13d50172009-10-11 08:08:02 +00001662Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1663 RegionBindings B = GetRegionBindings(store);
1664 llvm::OwningPtr<RegionStoreSubRegionMap>
1665 SubRegions(getRegionStoreSubRegionMap(store));
1666 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001667
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001668 // Set the default value of the struct region to "unknown".
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001669 B = Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001670
Zhongxing Xu13d50172009-10-11 08:08:02 +00001671 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001672}
1673
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001674const GRState*
1675RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1676 const GRState *state,
1677 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001678
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001679 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001680 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001681
Mike Stump1eb44332009-09-09 15:08:12 +00001682 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001683 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001684
Mike Stump1eb44332009-09-09 15:08:12 +00001685 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001686 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001688 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1689 // results in a zero-copy algorithm.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001690 return state->makeWithStore(Add(B, R,
1691 BindingVal(V, BindingVal::Direct)).getRoot());
1692}
1693
1694//===----------------------------------------------------------------------===//
1695// "Raw" retrievals and bindings.
1696//===----------------------------------------------------------------------===//
1697
1698RegionBindings RegionStoreManager::Add(RegionBindings B, BindingKey K,
1699 BindingVal V) {
1700 return RBFactory.Add(B, K, V);
1701}
1702
1703RegionBindings RegionStoreManager::Add(RegionBindings B, const MemRegion *R,
1704 BindingVal V) {
1705 return Add(B, BindingKey(R), V);
1706}
1707
1708const BindingVal *RegionStoreManager::Lookup(RegionBindings B, BindingKey K) {
1709 return B.lookup(K);
1710}
1711
1712const BindingVal *RegionStoreManager::Lookup(RegionBindings B,
1713 const MemRegion *R) {
1714 return Lookup(B, BindingKey(R));
1715}
1716
1717RegionBindings RegionStoreManager::Remove(RegionBindings B, BindingKey K) {
1718 return RBFactory.Remove(B, K);
1719}
1720
1721RegionBindings RegionStoreManager::Remove(RegionBindings B, const MemRegion *R){
1722 return Remove(B, BindingKey(R));
1723}
1724
1725Store RegionStoreManager::Remove(Store store, BindingKey K) {
1726 RegionBindings B = GetRegionBindings(store);
1727 return Remove(B, K).getRoot();
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001728}
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Ted Kremenek9af46f52009-06-16 22:36:44 +00001730//===----------------------------------------------------------------------===//
1731// State pruning.
1732//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001733
Mike Stump1eb44332009-09-09 15:08:12 +00001734void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001735 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001736 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001737{
Ted Kremenek781115c2009-10-17 17:45:11 +00001738 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1739
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001740 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001741 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Ted Kremenek9af46f52009-06-16 22:36:44 +00001743 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001744 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001745 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001746
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001747 // Do a pass over the regions in the store. For VarRegions we check if
1748 // the variable is still live and if so add it to the list of live roots.
1749 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001750 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001751
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001752 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001753 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001754 const MemRegion *R = I.getKey().getRegion();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001755 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001756 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001757
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001758 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001759 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001760 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001761 llvm::SmallVector<RBDNode, 10> Postponed;
1762
Zhongxing Xu6800b332009-10-18 04:15:47 +00001763 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001764
Ted Kremenek9af46f52009-06-16 22:36:44 +00001765 while (!IntermediateRoots.empty()) {
1766 const MemRegion* R = IntermediateRoots.back();
1767 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001768
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001769 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001770 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001771 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001772
Ted Kremenek9af46f52009-06-16 22:36:44 +00001773 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001774 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001775 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001776 continue;
1777 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001778
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001779 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001780 llvm::SmallVectorImpl<RBDNode> &Q =
1781 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1782
1783 Q.push_back(std::make_pair(&state, SR));
1784
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001785 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001786 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001787
1788 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001789 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001790 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001791 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001792 }
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001794 // Enqueue the RegionRoots onto WorkList.
1795 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1796 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001797 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001798 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001799 RegionRoots.clear();
1800
Zhongxing Xu6800b332009-10-18 04:15:47 +00001801 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001802
Ted Kremenek01756192009-10-29 05:14:17 +00001803tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001804 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001805 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001806 WorkList.pop_back();
1807
1808 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001809 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001810 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001811 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001813 const MemRegion *R = N.second;
1814 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001815
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001816 // Enqueue subregions.
1817 RegionStoreSubRegionMap *M;
1818
1819 if (&state == state_N)
1820 M = SubRegions.get();
1821 else {
1822 RegionStoreSubRegionMap *& SM = SC[state_N];
1823 if (!SM)
1824 SM = getRegionStoreSubRegionMap(state_N->getStore());
1825 M = SM;
1826 }
1827
1828 RegionStoreSubRegionMap::iterator I, E;
1829 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1830 WorkList.push_back(std::make_pair(state_N, *I));
1831
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001832 // Enqueue the super region.
1833 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1834 const MemRegion *superR = SR->getSuperRegion();
1835 if (!isa<MemSpaceRegion>(superR)) {
1836 // If 'R' is a field or an element, we want to keep the bindings
1837 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001838 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001839 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1840 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001841 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001842 }
1843 }
1844
1845 // Mark the symbol for any live SymbolicRegion as "live". This means we
1846 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001847 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001848 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001849
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001850 // For BlockDataRegions, enqueue the VarRegions for variables marked
1851 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001852 // via BlockDeclRefExprs.
1853 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1854 for (BlockDataRegion::referenced_vars_iterator
1855 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001856 RI != RE; ++RI) {
1857 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1858 WorkList.push_back(std::make_pair(state_N, *RI));
1859 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001860 // No possible data bindings on a BlockDataRegion. Continue to the
1861 // next region in the worklist.
1862 continue;
1863 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001864
1865 Store store_N = state_N->getStore();
1866 RegionBindings B_N = GetRegionBindings(store_N);
1867
1868 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001869 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001870
Zhongxing Xu13d50172009-10-11 08:08:02 +00001871 if (V) {
1872 // Check for lazy bindings.
1873 if (const nonloc::LazyCompoundVal *LCV =
1874 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001875
Zhongxing Xu13d50172009-10-11 08:08:02 +00001876 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001877 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001878 }
1879 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001880 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001881 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001882 SI!=SE;++SI)
1883 SymReaper.markLive(*SI);
1884
Zhongxing Xu13d50172009-10-11 08:08:02 +00001885 // If V is a region, then add it to the worklist.
1886 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001887 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001888 }
1889 }
1890 }
1891
Ted Kremenek01756192009-10-29 05:14:17 +00001892 // See if any postponed SymbolicRegions are actually live now, after
1893 // having done a scan.
1894 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1895 E = Postponed.end() ; I != E ; ++I) {
1896 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1897 if (SymReaper.isLive(SR->getSymbol())) {
1898 WorkList.push_back(*I);
1899 I->second = NULL;
1900 }
1901 }
1902 }
1903
1904 if (!WorkList.empty())
1905 goto tryAgain;
1906
Ted Kremenek9af46f52009-06-16 22:36:44 +00001907 // We have now scanned the store, marking reachable regions and symbols
1908 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001909 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001910 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001911 const MemRegion* R = I.getKey().getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001912 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001913 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001914 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Ted Kremenek9af46f52009-06-16 22:36:44 +00001916 // Remove this dead region from the store.
Ted Kremenek1c1ae6b2010-01-11 00:07:44 +00001917 store = Remove(store, I.getKey());
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Ted Kremenek9af46f52009-06-16 22:36:44 +00001919 // Mark all non-live symbols that this region references as dead.
1920 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1921 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Zhongxing Xu13d50172009-10-11 08:08:02 +00001923 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001924 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1925 for (; SI != SE; ++SI)
1926 SymReaper.maybeDead(*SI);
1927 }
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001929 // Write the store back.
1930 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001931}
1932
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001933GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1934 StackFrameContext const *frame) {
1935 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1936 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1937
1938 FunctionDecl::param_const_iterator PI = FD->param_begin();
1939
1940 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1941
1942 // Copy the arg expression value to the arg variables.
1943 for (; AI != AE; ++AI, ++PI) {
1944 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001945 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001946 }
1947
1948 return state;
1949}
1950
Ted Kremenek9af46f52009-06-16 22:36:44 +00001951//===----------------------------------------------------------------------===//
1952// Utility methods.
1953//===----------------------------------------------------------------------===//
1954
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001955void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001956 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001957 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001958 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Ted Kremenek451ac092009-08-06 04:50:20 +00001960 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001961 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001962}