blob: 46cddd0da1b3261a1b95846fc3136d424c6ea844 [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#include "llvm/Support/Compiler.h"
29
30using namespace clang;
31
Ted Kremenek356e9d62009-07-22 04:35:42 +000032#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000033#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000034
Zhongxing Xu13d50172009-10-11 08:08:02 +000035namespace {
36class BindingVal {
37public:
38 enum BindingKind { Direct, Default };
39private:
40 SVal Value;
41 BindingKind Kind;
42
43public:
44 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
45
46 bool isDefault() const { return Kind == Default; }
47
48 const SVal *getValue() const { return &Value; }
49
50 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
51
52 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
53
54 void Profile(llvm::FoldingSetNodeID& ID) const {
55 Value.Profile(ID);
56 ID.AddInteger(Kind);
57 }
58
59 inline bool operator==(const BindingVal& R) const {
60 return Value == R.Value && Kind == R.Kind;
61 }
62
63 inline bool operator!=(const BindingVal& R) const {
64 return !(*this == R);
65 }
66};
67}
68
69namespace llvm {
70static inline
71llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
72 if (V.isDefault())
73 os << "(default) ";
74 else
75 os << "(direct) ";
76 os << *V.getValue();
77 return os;
78}
79} // end llvm namespace
80
Zhongxing Xubaf03a72008-11-24 09:44:56 +000081// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000082typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000083
Ted Kremenek50dc1b32008-12-24 01:05:03 +000084//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000085// Fine-grained control of RegionStoreManager.
86//===----------------------------------------------------------------------===//
87
88namespace {
89struct VISIBILITY_HIDDEN minimal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000090struct VISIBILITY_HIDDEN maximal_features_tag {};
91
Ted Kremenek9af46f52009-06-16 22:36:44 +000092class VISIBILITY_HIDDEN RegionStoreFeatures {
93 bool SupportsFields;
94 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenek9af46f52009-06-16 22:36:44 +000096public:
97 RegionStoreFeatures(minimal_features_tag) :
98 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek9af46f52009-06-16 22:36:44 +0000100 RegionStoreFeatures(maximal_features_tag) :
101 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek9af46f52009-06-16 22:36:44 +0000105 bool supportsFields() const { return SupportsFields; }
106 bool supportsRemaining() const { return SupportsRemaining; }
107};
108}
109
110//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111// Region "Extents"
112//===----------------------------------------------------------------------===//
113//
114// MemRegions represent chunks of memory with a size (their "extent"). This
115// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000116//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
118static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000119namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000120 template<> struct GRStateTrait<RegionExtents>
121 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
122 static void* GDMIndex() { return &RegionExtentsIndex; }
123 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000124}
125
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000126//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000127// Utility functions.
128//===----------------------------------------------------------------------===//
129
130static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
131 if (ty->isAnyPointerType())
132 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000134 return ty->isIntegerType() && ty->isScalarType() &&
135 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
136}
137
138//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000139// Main RegionStore logic.
140//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000141
Zhongxing Xu17892752008-10-08 02:50:44 +0000142namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
145 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000146 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000147 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000150 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000151 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000152
153 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000154 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000155 return true;
156 }
157
158 I->second = F.Add(I->second, SubRegion);
159 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000162 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek59e8f112009-03-03 01:35:36 +0000164 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek5dc27462009-03-03 02:51:43 +0000166 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000167 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000168
169 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000170 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 llvm::ImmutableSet<const MemRegion*> S = I->second;
173 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
174 SI != SE; ++SI) {
175 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000176 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek5dc27462009-03-03 02:51:43 +0000179 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000182 typedef SetTy::iterator iterator;
183
184 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
185 Map::iterator I = M.find(R);
186 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
187 return std::make_pair(S.begin(), S.end());
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000190
Zhongxing Xu17892752008-10-08 02:50:44 +0000191class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000192 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000193 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000194
195 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
196 SMCache SC;
197
Zhongxing Xu17892752008-10-08 02:50:44 +0000198public:
Mike Stump1eb44332009-09-09 15:08:12 +0000199 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000200 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000202 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000203
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000204 virtual ~RegionStoreManager() {
205 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
206 delete (*I).second;
207 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000208
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000209 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Zhongxing Xu13d50172009-10-11 08:08:02 +0000211 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Zhongxing Xu13d50172009-10-11 08:08:02 +0000213 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
214 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000215 /// getDefaultBinding - Returns an SVal* representing an optional default
216 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000217 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000219 /// getLValueString - Returns an SVal representing the lvalue of a
220 /// StringLiteral. Within RegionStore a StringLiteral has an
221 /// associated StringRegion, and the lvalue of a StringLiteral is
222 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000223 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000224
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000225 /// getLValueCompoundLiteral - Returns an SVal representing the
226 /// lvalue of a compound literal. Within RegionStore a compound
227 /// literal has an associated region, and the lvalue of the
228 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000229 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000230
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000231 /// getLValueVar - Returns an SVal that represents the lvalue of a
232 /// variable. Within RegionStore a variable has an associated
233 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000234 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000236 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000237
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000238 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000240 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000241
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000242 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000243
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000244
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000245 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
246 /// type. 'Array' represents the lvalue of the array being decayed
247 /// to a pointer, and the returned SVal represents the decayed
248 /// version of that lvalue (i.e., a pointer to the first element of
249 /// the array). This is called by GRExprEngine when evaluating
250 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000251 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000252
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000253 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000254 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000255
Mike Stump1eb44332009-09-09 15:08:12 +0000256 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000257 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000258 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000259
Ted Kremenek67f28532009-06-17 22:02:04 +0000260 //===-------------------------------------------------------------------===//
261 // Binding values to regions.
262 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000263
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000264 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000265 const Expr *E, unsigned Count,
266 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000268private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000269 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000270 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
272public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000273 const GRState *Bind(const GRState *state, Loc LV, SVal V);
274
275 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000276 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000278 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
279 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000280
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000281 const GRState *BindDeclWithNoInit(const GRState *state,
282 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000283 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000284 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000285
Ted Kremenek67f28532009-06-17 22:02:04 +0000286 /// BindStruct - Bind a compound value to a structure.
287 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Ted Kremenek67f28532009-06-17 22:02:04 +0000289 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
291 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000292 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000293
Ted Kremenek67f28532009-06-17 22:02:04 +0000294 Store Remove(Store store, Loc LV);
295
296 //===------------------------------------------------------------------===//
297 // Loading values from regions.
298 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 /// The high level logic for this method is this:
301 /// Retrieve (L)
302 /// if L has binding
303 /// return L's binding
304 /// else if L is in killset
305 /// return unknown
306 /// else
307 /// if L is on stack or heap
308 /// return undefined
309 /// else
310 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000311 SValuator::CastResult Retrieve(const GRState *state, Loc L,
312 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000313
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000314 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000315
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000316 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000318 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremenek9031dd72009-07-21 00:12:07 +0000320 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Ted Kremenek25c54572009-07-20 22:58:02 +0000322 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000324 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
325 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremenek67f28532009-06-17 22:02:04 +0000327 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000328 /// struct copy:
329 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000330 /// x = y;
331 /// y's value is retrieved by this method.
332 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek67f28532009-06-17 22:02:04 +0000334 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000336 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000337 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000339 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
340 const GRState *state,
341 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000342
Ted Kremenek0954cde2009-09-24 04:11:44 +0000343 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
344 QualType T);
345
Ted Kremenek67f28532009-06-17 22:02:04 +0000346 //===------------------------------------------------------------------===//
347 // State pruning.
348 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek67f28532009-06-17 22:02:04 +0000350 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
351 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000352 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
354
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000355 const GRState *EnterStackFrame(const GRState *state,
356 const StackFrameContext *frame);
357
Ted Kremenek67f28532009-06-17 22:02:04 +0000358 //===------------------------------------------------------------------===//
359 // Region "extents".
360 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek67f28532009-06-17 22:02:04 +0000362 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000363 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
364 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000365
366 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000367 // Utility methods.
368 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek451ac092009-08-06 04:50:20 +0000370 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000371 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000372 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000373
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000374 void print(Store store, llvm::raw_ostream& Out, const char* nl,
375 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000376
377 void iterBindings(Store store, BindingsHandler& f) {
378 // FIXME: Implement.
379 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000380
Ted Kremenek67f28532009-06-17 22:02:04 +0000381 // FIXME: Remove.
382 BasicValueFactory& getBasicVals() {
383 return StateMgr.getBasicVals();
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek67f28532009-06-17 22:02:04 +0000386 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000387 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000388};
389
390} // end anonymous namespace
391
Ted Kremenek9af46f52009-06-16 22:36:44 +0000392//===----------------------------------------------------------------------===//
393// RegionStore creation.
394//===----------------------------------------------------------------------===//
395
396StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
397 RegionStoreFeatures F = maximal_features_tag();
398 return new RegionStoreManager(StMgr, F);
399}
400
401StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
402 RegionStoreFeatures F = minimal_features_tag();
403 F.enableFields(true);
404 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000405}
406
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000407void
408RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000409 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000410 const MemRegion *superR = R->getSuperRegion();
411 if (add(superR, R))
412 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000413 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414}
415
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000416RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000417RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
418 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000419 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000421 llvm::SmallVector<const SubRegion*, 10> WL;
422
Ted Kremenek451ac092009-08-06 04:50:20 +0000423 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000424 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
425 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428 // don't have direct bindings but are super regions of those that do.
429 while (!WL.empty()) {
430 const SubRegion *R = WL.back();
431 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000432 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000433 }
434
Ted Kremenek14453bf2009-03-03 19:02:42 +0000435 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000436}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000437
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000438SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000439 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000440}
441
Ted Kremenek9af46f52009-06-16 22:36:44 +0000442//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000443// Binding invalidation.
444//===----------------------------------------------------------------------===//
445
Zhongxing Xu13d50172009-10-11 08:08:02 +0000446void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
447 const MemRegion *R,
448 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000449 RegionStoreSubRegionMap::iterator I, E;
450
451 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000452 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000454 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000455}
456
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000457const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
458 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000459 const Expr *Ex,
Ted Kremenek473e1672009-10-16 00:30:49 +0000460 unsigned Count,
461 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000462 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000464 // Strip away casts.
Zhongxing Xu479529e2009-11-10 02:17:20 +0000465 R = R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000466
Ted Kremenek87806792009-09-27 20:45:21 +0000467 // Get the mapping of regions -> subregions.
468 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000469 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000470
471 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000472
Ted Kremenek87806792009-09-27 20:45:21 +0000473 llvm::DenseMap<const MemRegion *, unsigned> Visited;
474 llvm::SmallVector<const MemRegion *, 10> WorkList;
475 WorkList.push_back(R);
476
477 while (!WorkList.empty()) {
478 R = WorkList.back();
479 WorkList.pop_back();
480
481 // Have we visited this region before?
482 unsigned &visited = Visited[R];
483 if (visited)
484 continue;
485 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Ted Kremenek87806792009-09-27 20:45:21 +0000487 // Add subregions to work list.
488 RegionStoreSubRegionMap::iterator I, E;
489 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
490 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000491
492 // Get the old binding. Is it a region? If so, add it to the worklist.
493 if (Optional<SVal> V = getDirectBinding(B, R)) {
494 if (const MemRegion *RV = V->getAsRegion())
495 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000496
497 // A symbol? Mark it touched by the invalidation.
498 if (IS) {
499 if (SymbolRef Sym = V->getAsSymbol())
500 IS->insert(Sym);
501 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000502 }
503
Ted Kremenek473e1672009-10-16 00:30:49 +0000504 // Symbolic region? Mark that symbol touched by the invalidation.
505 if (IS) {
506 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
507 IS->insert(SR->getSymbol());
508 }
509
510 // Handle the region itself.
Ted Kremenek87806792009-09-27 20:45:21 +0000511 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
512 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000513 // Invalidate the region by setting its default value to
514 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000515 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
516 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000517 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000518 continue;
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Ted Kremenek87806792009-09-27 20:45:21 +0000521 if (!R->isBoundable())
522 continue;
523
524 const TypedRegion *TR = cast<TypedRegion>(R);
525 QualType T = TR->getValueType(Ctx);
526
527 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000528 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
529
Zhongxing Xu13d50172009-10-11 08:08:02 +0000530 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000531 if (!RD)
532 continue;
533
Zhongxing Xu13d50172009-10-11 08:08:02 +0000534 // Invalidate the region by setting its default value to
535 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000536 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
537 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000538 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000539 continue;
540 }
541
542 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
543 // Set the default value of the array to conjured symbol.
544 DefinedOrUnknownSVal V =
545 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000546 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000547 continue;
548 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000549
550 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
551 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000552 // For fields and elements whose super region has also been invalidated,
553 // only remove the old binding. The super region will get set with a
554 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000555 B = RBFactory.Remove(B, R);
556 continue;
557 }
Ted Kremenek87806792009-09-27 20:45:21 +0000558
Ted Kremenek389c44c2009-09-29 03:12:50 +0000559 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000560 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
561 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000562 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000563 }
564
Ted Kremenek87806792009-09-27 20:45:21 +0000565 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000566 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000567}
568
569//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000570// getLValueXXX methods.
571//===----------------------------------------------------------------------===//
572
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000573/// getLValueString - Returns an SVal representing the lvalue of a
574/// StringLiteral. Within RegionStore a StringLiteral has an
575/// associated StringRegion, and the lvalue of a StringLiteral is the
576/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000577SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000578 return loc::MemRegionVal(MRMgr.getStringRegion(S));
579}
580
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000581/// getLValueVar - Returns an SVal that represents the lvalue of a
582/// variable. Within RegionStore a variable has an associated
583/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000584SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000585 const LocationContext *LC) {
586 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000587}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000588
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000589/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
590/// of a compound literal. Within RegionStore a compound literal
591/// has an associated region, and the lvalue of the compound literal
592/// is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000593SVal
594RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000595 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
596}
597
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000598SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
599 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000600}
601
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000602SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
603 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000604}
605
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000606SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000607 if (Base.isUnknownOrUndef())
608 return Base;
609
610 Loc BaseL = cast<Loc>(Base);
611 const MemRegion* BaseR = 0;
612
613 switch (BaseL.getSubKind()) {
614 case loc::MemRegionKind:
615 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
616 break;
617
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000618 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000619 // These are anormal cases. Flag an undefined value.
620 return UndefinedVal();
621
622 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000623 // While these seem funny, this can happen through casts.
624 // FIXME: What we should return is the field offset. For example,
625 // add the field offset to the integer value. That way funny things
626 // like this work properly: &(((struct foo *) 0xa)->f)
627 return Base;
628
629 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000630 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000631 return Base;
632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000634 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
635 // of FieldDecl.
636 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
637 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000638
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000639 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000640}
641
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000642SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
643 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000644
Ted Kremenekde7ec632009-03-09 22:44:49 +0000645 // If the base is an unknown or undefined value, just return it back.
646 // FIXME: For absolute pointer addresses, we just return that value back as
647 // well, although in reality we should return the offset added to that
648 // value.
649 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000650 return Base;
651
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000652 // Only handle integer offsets... for now.
653 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000654 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000655
Zhongxing Xuce760782009-05-09 13:20:07 +0000656 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000657
658 // Pointer of any type can be cast and used as array base.
659 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek46537392009-07-16 01:33:37 +0000661 // Convert the offset to the appropriate size and signedness.
662 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000664 if (!ElemR) {
665 //
666 // If the base region is not an ElementRegion, create one.
667 // This can happen in the following example:
668 //
669 // char *p = __builtin_alloc(10);
670 // p[1] = 8;
671 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000672 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000673 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000674 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000675 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000676 }
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000678 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000680 if (!isa<nonloc::ConcreteInt>(BaseIdx))
681 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000683 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
684 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
685 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremenek46537392009-07-16 01:33:37 +0000687 // Compute the new index.
688 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek46537392009-07-16 01:33:37 +0000690 // Construct the new ElementRegion.
691 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000692 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000693 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000694}
695
Ted Kremenek9af46f52009-06-16 22:36:44 +0000696//===----------------------------------------------------------------------===//
697// Extents for regions.
698//===----------------------------------------------------------------------===//
699
Zhongxing Xue884ff82009-11-12 02:48:32 +0000700DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
701 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703 switch (R->getKind()) {
704 case MemRegion::MemSpaceRegionKind:
705 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000706 return UnknownVal();
707
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000708 case MemRegion::CodeTextRegionKind:
709 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000710 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000711
712 // Not yet handled.
713 case MemRegion::AllocaRegionKind:
714 case MemRegion::CompoundLiteralRegionKind:
715 case MemRegion::ElementRegionKind:
716 case MemRegion::FieldRegionKind:
717 case MemRegion::ObjCIvarRegionKind:
718 case MemRegion::ObjCObjectRegionKind:
719 case MemRegion::SymbolicRegionKind:
720 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000722 case MemRegion::StringRegionKind: {
723 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000724 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000725 // operations with signed indices.
726 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000729 case MemRegion::VarRegionKind: {
730 const VarRegion* VR = cast<VarRegion>(R);
731 // Get the type of the variable.
732 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000734 // FIXME: Handle variable-length arrays.
735 if (isa<VariableArrayType>(T))
736 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000738 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
739 // return the size as signed integer.
740 return ValMgr.makeIntVal(CAT->getSize(), false);
741 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000742
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000743 // Clients can use ordinary variables as if they were arrays. These
744 // essentially are arrays of size 1.
745 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000748 case MemRegion::BEG_DECL_REGIONS:
749 case MemRegion::END_DECL_REGIONS:
750 case MemRegion::BEG_TYPED_REGIONS:
751 case MemRegion::END_TYPED_REGIONS:
752 assert(0 && "Infeasible region");
753 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000756 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000757 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000758}
759
Ted Kremenek67f28532009-06-17 22:02:04 +0000760const GRState *RegionStoreManager::setExtent(const GRState *state,
761 const MemRegion *region,
762 SVal extent) {
763 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000764}
765
766//===----------------------------------------------------------------------===//
767// Location and region casting.
768//===----------------------------------------------------------------------===//
769
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000770/// ArrayToPointer - Emulates the "decay" of an array to a pointer
771/// type. 'Array' represents the lvalue of the array being decayed
772/// to a pointer, and the returned SVal represents the decayed
773/// version of that lvalue (i.e., a pointer to the first element of
774/// the array). This is called by GRExprEngine when evaluating casts
775/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000776SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000777 if (!isa<loc::MemRegionVal>(Array))
778 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenekabb042f2008-12-13 19:24:37 +0000780 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
781 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000783 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000784 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000786 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000787 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000788 ArrayType *AT = cast<ArrayType>(T);
789 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenek75185b52009-07-16 00:00:11 +0000791 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
792 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000793
794 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000795}
796
Ted Kremenek9af46f52009-06-16 22:36:44 +0000797//===----------------------------------------------------------------------===//
798// Pointer arithmetic.
799//===----------------------------------------------------------------------===//
800
Mike Stump1eb44332009-09-09 15:08:12 +0000801SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000802 BinaryOperator::Opcode Op, Loc L, NonLoc R,
803 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000804 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000805 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000806 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000807
Zhongxing Xua1718c72009-04-03 07:33:13 +0000808 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000809 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000810
Ted Kremenek3bccf082009-07-11 00:58:27 +0000811 switch (MR->getKind()) {
812 case MemRegion::SymbolicRegionKind: {
813 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000814 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000815 QualType T = Sym->getType(getContext());
816 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000818 if (const PointerType *PT = T->getAs<PointerType>())
819 EleTy = PT->getPointeeType();
820 else
John McCall183700f2009-09-21 23:43:11 +0000821 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Ted Kremenek3bccf082009-07-11 00:58:27 +0000823 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
824 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000825 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000826 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000827 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000828 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000829 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000830 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000831 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
832 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000833 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000834 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000835
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 case MemRegion::ElementRegionKind: {
837 ER = cast<ElementRegion>(MR);
838 break;
839 }
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 // Not yet handled.
842 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000843 case MemRegion::StringRegionKind: {
844
845 }
846 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000847 case MemRegion::CompoundLiteralRegionKind:
848 case MemRegion::FieldRegionKind:
849 case MemRegion::ObjCObjectRegionKind:
850 case MemRegion::ObjCIvarRegionKind:
851 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek3bccf082009-07-11 00:58:27 +0000853 case MemRegion::CodeTextRegionKind:
854 // Technically this can happen if people do funny things with casts.
855 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Ted Kremenek3bccf082009-07-11 00:58:27 +0000857 case MemRegion::MemSpaceRegionKind:
858 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
859 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenek3bccf082009-07-11 00:58:27 +0000861 case MemRegion::BEG_DECL_REGIONS:
862 case MemRegion::END_DECL_REGIONS:
863 case MemRegion::BEG_TYPED_REGIONS:
864 case MemRegion::END_TYPED_REGIONS:
865 assert(0 && "Infeasible region");
866 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000867 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000868
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000869 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000870 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000871
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000872 // For now, only support:
873 // (a) concrete integer indices that can easily be resolved
874 // (b) 0 + symbolic index
875 if (Base) {
876 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
877 // FIXME: Should use SValuator here.
878 SVal NewIdx =
879 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000880 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000881 const MemRegion* NewER =
882 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
883 ER->getSuperRegion(), getContext());
884 return ValMgr.makeLoc(NewER);
885 }
886 if (0 == Base->getValue()) {
887 const MemRegion* NewER =
888 MRMgr.getElementRegion(ER->getElementType(), R,
889 ER->getSuperRegion(), getContext());
890 return ValMgr.makeLoc(NewER);
891 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Ted Kremenek5dc27462009-03-03 02:51:43 +0000894 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000895}
896
Ted Kremenek9af46f52009-06-16 22:36:44 +0000897//===----------------------------------------------------------------------===//
898// Loading values from regions.
899//===----------------------------------------------------------------------===//
900
Zhongxing Xu13d50172009-10-11 08:08:02 +0000901Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
902 const MemRegion *R) {
903 if (const BindingVal *BV = B.lookup(R))
904 return Optional<SVal>::create(BV->getDirectValue());
905
906 return Optional<SVal>();
907}
908
909Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000910 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000912 if (R->isBoundable())
913 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
914 if (TR->getValueType(getContext())->isUnionType())
915 return UnknownVal();
916
Zhongxing Xu13d50172009-10-11 08:08:02 +0000917 if (BindingVal const *V = B.lookup(R))
918 return Optional<SVal>::create(V->getDefaultValue());
919
920 return Optional<SVal>();
921}
922
923Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
924 const MemRegion *R) {
925 if (const BindingVal *BV = B.lookup(R))
926 return Optional<SVal>::create(BV->getValue());
927
928 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000929}
930
Ted Kremeneka6275a52009-07-15 02:31:43 +0000931static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
932 RTy = Ctx.getCanonicalType(RTy);
933 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Ted Kremeneka6275a52009-07-15 02:31:43 +0000935 if (RTy == UsedTy)
936 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
938
Ted Kremenek25c54572009-07-20 22:58:02 +0000939 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000940 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000941 // represents a reinterpretation.
942 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000944 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000945
946 return PUsedTy && PRTy &&
947 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000948 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000949 }
950
951 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000952}
953
Ted Kremenek0954cde2009-09-24 04:11:44 +0000954const ElementRegion *
955RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
956 ASTContext &Ctx = getContext();
957 SVal idx = ValMgr.makeZeroArrayIndex();
958 assert(!T.isNull());
959 return MRMgr.getElementRegion(T, idx, SR, Ctx);
960}
961
962
963
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000964SValuator::CastResult
965RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000966
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000967 assert(!isa<UnknownVal>(L) && "location unknown");
968 assert(!isa<UndefinedVal>(L) && "location undefined");
969
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000970 // FIXME: Is this even possible? Shouldn't this be treated as a null
971 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000972 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000973 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000974
Ted Kremenek67f28532009-06-17 22:02:04 +0000975 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000976
Zhongxing Xu91844122009-05-20 09:18:48 +0000977 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000978 // Example:
979 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000980 // char* p = alloca();
981 // read(p);
982 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000983 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000984 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Ted Kremenek0954cde2009-09-24 04:11:44 +0000986 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
987 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Ted Kremenek968f0a62009-08-03 21:41:46 +0000989 if (isa<CodeTextRegion>(MR))
990 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000992 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
993 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000994 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000995 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000996
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000997 // FIXME: We should eventually handle funny addressing. e.g.:
998 //
999 // int x = ...;
1000 // int *p = &x;
1001 // char *q = (char*) p;
1002 // char c = *q; // returns the first byte of 'x'.
1003 //
1004 // Such funny addressing will occur due to layering of regions.
1005
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001006#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001007 ASTContext &Ctx = getContext();
1008 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001009 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1010 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001011 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001012 assert(Ctx.getCanonicalType(RTy) ==
1013 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001014 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001015#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001016
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001017 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001018 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001020 // FIXME: Handle unions.
1021 if (RTy->isUnionType())
1022 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001023
1024 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001025 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001026
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001027 // FIXME: handle Vector types.
1028 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001029 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001030
1031 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001032 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001033
1034 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001035 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Ted Kremenek25c54572009-07-20 22:58:02 +00001037 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001038 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Ted Kremenek9031dd72009-07-21 00:12:07 +00001040 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001041 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001042
Ted Kremenek451ac092009-08-06 04:50:20 +00001043 RegionBindings B = GetRegionBindings(state->getStore());
1044 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001045
1046 // Check if the region has a binding.
1047 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001048 if (SVal const *SV = V->getValue())
1049 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001050
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001051 // The location does not have a bound value. This means that it has
1052 // the value it had upon its creation and/or entry to the analyzed
1053 // function/method. These are either symbolic values or 'undefined'.
1054
Ted Kremenek356e9d62009-07-22 04:35:42 +00001055#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001056 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001057#else
1058 if (R->hasStackStorage()) {
1059#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001060 // All stack variables are considered to have undefined values
1061 // upon creation. All heap allocated blocks are considered to
1062 // have undefined values as well unless they are explicitly bound
1063 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001064 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001065 }
1066
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001067 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001068 return SValuator::CastResult(state,
1069 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001070}
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001072std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001073RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001074 if (Optional<SVal> OV = getDirectBinding(B, R))
1075 if (const nonloc::LazyCompoundVal *V =
1076 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1077 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001079 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1080 const std::pair<const GRState *, const MemRegion *> &X =
1081 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001083 if (X.first)
1084 return std::make_pair(X.first,
1085 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001086 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001087 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1088 const std::pair<const GRState *, const MemRegion *> &X =
1089 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001091 if (X.first)
1092 return std::make_pair(X.first,
1093 MRMgr.getFieldRegionWithSuper(FR, X.second));
1094 }
1095
1096 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1097}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001098
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001099SVal RegionStoreManager::RetrieveElement(const GRState* state,
1100 const ElementRegion* R) {
1101 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001102 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001103 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001104 return *V;
1105
Ted Kremenek921109a2009-07-01 23:19:52 +00001106 const MemRegion* superR = R->getSuperRegion();
1107
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001108 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001109 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001110 // FIXME: Handle loads from strings where the literal is treated as
1111 // an integer, e.g., *((unsigned int*)"hello")
1112 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001113 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001114 if (T != Ctx.getCanonicalType(R->getElementType()))
1115 return UnknownVal();
1116
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001117 const StringLiteral *Str = StrR->getStringLiteral();
1118 SVal Idx = R->getIndex();
1119 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1120 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001121 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001122 if (i > byteLength) {
1123 // Buffer overflow checking in GRExprEngine should handle this case,
1124 // but we shouldn't rely on it to not overflow here if that checking
1125 // is disabled.
1126 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001127 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001128 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001129 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001130 }
1131 }
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001133 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001134 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001135 if (SymbolRef parentSym = V->getAsSymbol())
1136 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001137
1138 if (V->isUnknownOrUndef())
1139 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001140
1141 // Handle LazyCompoundVals for the immediate super region. Other cases
1142 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001143 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001144 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001146 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1147 return RetrieveElement(LCV->getState(), R);
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Ted Kremeneka6275a52009-07-15 02:31:43 +00001150 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001151 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001152 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001153
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001154 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001155}
1156
Mike Stump1eb44332009-09-09 15:08:12 +00001157SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001158 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001159
1160 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001161 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001162 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001163 return *V;
1164
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001165 QualType Ty = R->getValueType(getContext());
1166 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1167}
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001169SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1170 const TypedRegion *R,
1171 QualType Ty,
1172 const MemRegion *superR) {
1173
Mike Stump1eb44332009-09-09 15:08:12 +00001174 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001175 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001177 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001179 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001180 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001181 if (SymbolRef parentSym = D->getAsSymbol())
1182 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001184 if (D->isZeroConstant())
1185 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001187 if (D->isUnknown())
1188 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001190 assert(0 && "Unknown default value");
1191 }
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001193 // If our super region is a field or element itself, walk up the region
1194 // hierarchy to see if there is a default value installed in an ancestor.
1195 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1196 superR = cast<SubRegion>(superR)->getSuperRegion();
1197 continue;
1198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001200 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001203 // Lazy binding?
1204 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001205 const MemRegion *lazyBindingRegion = NULL;
1206 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001208 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001209 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001211 if (isa<ElementRegion>(R))
1212 return RetrieveElement(lazyBindingState,
1213 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001215 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001216 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001217 }
1218
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001219 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001221 if (isa<ElementRegion>(R)) {
1222 // Currently we don't reason specially about Clang-style vectors. Check
1223 // if superR is a vector and if so return Unknown.
1224 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1225 if (typedSuperR->getValueType(getContext())->isVectorType())
1226 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001227 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001228 }
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001230 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001231 }
Mike Stump1eb44332009-09-09 15:08:12 +00001232
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001233 // All other values are symbolic.
1234 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001235}
Mike Stump1eb44332009-09-09 15:08:12 +00001236
1237SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001238 const ObjCIvarRegion* R) {
1239
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001240 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001241 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001242
Zhongxing Xu13d50172009-10-11 08:08:02 +00001243 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001244 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001246 const MemRegion *superR = R->getSuperRegion();
1247
Ted Kremenekab22ee92009-10-20 01:20:57 +00001248 // Check if the super region has a default binding.
1249 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001250 if (SymbolRef parentSym = V->getAsSymbol())
1251 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001253 // Other cases: give up.
1254 return UnknownVal();
1255 }
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek25c54572009-07-20 22:58:02 +00001257 return RetrieveLazySymbol(state, R);
1258}
1259
Ted Kremenek9031dd72009-07-21 00:12:07 +00001260SVal RegionStoreManager::RetrieveVar(const GRState *state,
1261 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenek9031dd72009-07-21 00:12:07 +00001263 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001264 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Zhongxing Xu13d50172009-10-11 08:08:02 +00001266 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001267 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek9031dd72009-07-21 00:12:07 +00001269 // Lazily derive a value for the VarRegion.
1270 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremenek9031dd72009-07-21 00:12:07 +00001272 if (R->hasGlobalsOrParametersStorage())
1273 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Ted Kremenek9031dd72009-07-21 00:12:07 +00001275 return UndefinedVal();
1276}
1277
Mike Stump1eb44332009-09-09 15:08:12 +00001278SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001279 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Ted Kremenek25c54572009-07-20 22:58:02 +00001281 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001282
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001283 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001284 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001285}
1286
Mike Stump1eb44332009-09-09 15:08:12 +00001287SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1288 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001289 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001290 assert(T->isStructureType());
1291
Zhongxing Xub7507d12009-06-11 07:27:30 +00001292 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001293 RecordDecl* RD = RT->getDecl();
1294 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001295 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001296#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001297 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1298
Ted Kremenek67f28532009-06-17 22:02:04 +00001299 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1300 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001301 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001302
Douglas Gregore267ff32008-12-11 20:41:00 +00001303 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1304 FieldEnd = Fields.rend();
1305 Field != FieldEnd; ++Field) {
1306 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001307 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001308 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001309 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1310 }
1311
Zhongxing Xud91ee272009-06-23 09:02:15 +00001312 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001313#else
1314 return ValMgr.makeLazyCompoundVal(state, R);
1315#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001316}
1317
Ted Kremenek67f28532009-06-17 22:02:04 +00001318SVal RegionStoreManager::RetrieveArray(const GRState *state,
1319 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001320#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001321 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001322 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1323
1324 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001325 uint64_t size = CAT->getSize().getZExtValue();
1326 for (uint64_t i = 0; i < size; ++i) {
1327 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001328 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001329 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001330 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001331 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001332 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1333 }
1334
Zhongxing Xud91ee272009-06-23 09:02:15 +00001335 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001336#else
1337 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1338 return ValMgr.makeLazyCompoundVal(state, R);
1339#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001340}
1341
Ted Kremenek9af46f52009-06-16 22:36:44 +00001342//===----------------------------------------------------------------------===//
1343// Binding values to regions.
1344//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001345
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001346Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001347 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Ted Kremenek0964a062009-01-21 06:57:53 +00001349 if (isa<loc::MemRegionVal>(L))
1350 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Ted Kremenek0964a062009-01-21 06:57:53 +00001352 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001353 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001354 return RBFactory.Remove(B, R).getRoot();
1355 }
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Ted Kremenek0964a062009-01-21 06:57:53 +00001357 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001358}
1359
Ted Kremenek67f28532009-06-17 22:02:04 +00001360const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001361 if (isa<loc::ConcreteInt>(L))
1362 return state;
1363
Ted Kremenek9af46f52009-06-16 22:36:44 +00001364 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001365 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Ted Kremenek9af46f52009-06-16 22:36:44 +00001367 // Check if the region is a struct region.
1368 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1369 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001370 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001372 // Special case: the current region represents a cast and it and the super
1373 // region both have pointer types or intptr_t types. If so, perform the
1374 // bind to the super region.
1375 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001376 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001377 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1378 if (ER->getIndex().isZeroConstant()) {
1379 if (const TypedRegion *superR =
1380 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1381 ASTContext &Ctx = getContext();
1382 QualType superTy = superR->getValueType(Ctx);
1383 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
1385 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001386 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001387 SValuator::CastResult cr =
1388 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001389 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1390 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001391 // For now, just invalidate the fields of the struct/union/class.
1392 // FIXME: Precisely handle the fields of the record.
1393 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001394 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001395 }
1396 }
1397 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001398 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1399 // Binding directly to a symbolic region should be treated as binding
1400 // to element 0.
1401 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001402 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001403 R = GetElementZeroRegion(SR, T);
1404 }
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001406 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001407 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001408 return state->makeWithStore(
1409 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001410}
1411
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001412const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001413 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001414 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001415
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001416 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001417
Ted Kremenek0964a062009-01-21 06:57:53 +00001418 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001419 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001420 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001421 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001422
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001423 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001424}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001425
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001426// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001427const GRState *
1428RegionStoreManager::BindCompoundLiteral(const GRState *state,
1429 const CompoundLiteralExpr* CL,
1430 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001432 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001433 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001434}
1435
Ted Kremenek67f28532009-06-17 22:02:04 +00001436const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001437 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001438 SVal Init) {
1439
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001440 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001441 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001442 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001443
Ted Kremenek46537392009-07-16 01:33:37 +00001444 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001445
1446 // Check if the init expr is a StringLiteral.
1447 if (isa<loc::MemRegionVal>(Init)) {
1448 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1449 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1450 const char* str = S->getStrData();
1451 unsigned len = S->getByteLength();
1452 unsigned j = 0;
1453
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001454 // Copy bytes from the string literal into the target array. Trailing bytes
1455 // in the array that are not covered by the string literal are initialized
1456 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001457 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001458 if (j >= len)
1459 break;
1460
Ted Kremenek46537392009-07-16 01:33:37 +00001461 SVal Idx = ValMgr.makeArrayIndex(i);
1462 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1463 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001464
Zhongxing Xud91ee272009-06-23 09:02:15 +00001465 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001466 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001467 }
1468
Ted Kremenek67f28532009-06-17 22:02:04 +00001469 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001470 }
1471
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001472 // Handle lazy compound values.
1473 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1474 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001475
1476 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001477 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001478 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001479 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001480
Ted Kremenek46537392009-07-16 01:33:37 +00001481 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001482 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001483 if (VI == VE)
1484 break;
1485
Ted Kremenek46537392009-07-16 01:33:37 +00001486 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001487 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001488
1489 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001490 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001491 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001492 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001493 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001494 }
1495
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001496 // If the init list is shorter than the array length, set the array default
1497 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001498 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001499 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001500 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001501 Store store = state->getStore();
1502 RegionBindings B = GetRegionBindings(store);
1503 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1504 state = state->makeWithStore(B.getRoot());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001505 }
1506 }
1507
Ted Kremenek67f28532009-06-17 22:02:04 +00001508 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001509}
1510
Ted Kremenek67f28532009-06-17 22:02:04 +00001511const GRState *
1512RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1513 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek67f28532009-06-17 22:02:04 +00001515 if (!Features.supportsFields())
1516 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001518 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001519 assert(T->isStructureType());
1520
Ted Kremenek6217b802009-07-29 21:53:49 +00001521 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001522 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001523
1524 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001525 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001526
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001527 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001528 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001529 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek67f28532009-06-17 22:02:04 +00001531 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1532 // Ignore them and kill the field values.
1533 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001534 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001535
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001536 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001537 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001538
1539 RecordDecl::field_iterator FI, FE;
1540
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001541 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001542
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001543 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001544 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001545
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001546 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001547 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001548
Ted Kremenekcf549592009-09-22 21:19:14 +00001549 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001550 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001551 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001552 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001553 else
1554 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001555 }
1556
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001557 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001558 if (FI != FE) {
1559 Store store = state->getStore();
1560 RegionBindings B = GetRegionBindings(store);
1561 B = RBFactory.Add(B, R,
1562 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1563 state = state->makeWithStore(B.getRoot());
1564 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001565
Ted Kremenek67f28532009-06-17 22:02:04 +00001566 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001567}
1568
Zhongxing Xu13d50172009-10-11 08:08:02 +00001569Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1570 RegionBindings B = GetRegionBindings(store);
1571 llvm::OwningPtr<RegionStoreSubRegionMap>
1572 SubRegions(getRegionStoreSubRegionMap(store));
1573 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001574
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001575 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001576 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001577
Zhongxing Xu13d50172009-10-11 08:08:02 +00001578 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001579}
1580
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001581const GRState*
1582RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1583 const GRState *state,
1584 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001585
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001586 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001587 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001588
Mike Stump1eb44332009-09-09 15:08:12 +00001589 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001590 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001591
Mike Stump1eb44332009-09-09 15:08:12 +00001592 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001593 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001595 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1596 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001597 return state->makeWithStore(
1598 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001599}
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Ted Kremenek9af46f52009-06-16 22:36:44 +00001601//===----------------------------------------------------------------------===//
1602// State pruning.
1603//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001604
Mike Stump1eb44332009-09-09 15:08:12 +00001605void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001606 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001607 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001608{
Ted Kremenek781115c2009-10-17 17:45:11 +00001609 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1610
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001611 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001612 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Ted Kremenek9af46f52009-06-16 22:36:44 +00001614 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001615 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001616 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001617
1618 // Do a pass over the regions in the store. For VarRegions we check if
1619 // the variable is still live and if so add it to the list of live roots.
1620 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001621 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001622
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001623 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001624 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001625 const MemRegion *R = I.getKey();
1626 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001627 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001628
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001629 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001630 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001631 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001632 llvm::SmallVector<RBDNode, 10> Postponed;
1633
Zhongxing Xu6800b332009-10-18 04:15:47 +00001634 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001635
Ted Kremenek9af46f52009-06-16 22:36:44 +00001636 while (!IntermediateRoots.empty()) {
1637 const MemRegion* R = IntermediateRoots.back();
1638 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001639
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001640 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001641 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001642 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001643
Ted Kremenek9af46f52009-06-16 22:36:44 +00001644 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001645 if (SymReaper.isLive(Loc, VR->getDecl()))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001646 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001647 continue;
1648 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001649
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001650 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001651 llvm::SmallVectorImpl<RBDNode> &Q =
1652 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1653
1654 Q.push_back(std::make_pair(&state, SR));
1655
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001656 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001657 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001658
1659 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001660 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001661 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001662 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001665 // Enqueue the RegionRoots onto WorkList.
1666 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1667 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001668 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001669 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001670 RegionRoots.clear();
1671
Zhongxing Xu6800b332009-10-18 04:15:47 +00001672 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001673
Ted Kremenek01756192009-10-29 05:14:17 +00001674tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001675 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001676 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001677 WorkList.pop_back();
1678
1679 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001680 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001681 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001682 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001684 const MemRegion *R = N.second;
1685 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001686
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001687 // Enqueue subregions.
1688 RegionStoreSubRegionMap *M;
1689
1690 if (&state == state_N)
1691 M = SubRegions.get();
1692 else {
1693 RegionStoreSubRegionMap *& SM = SC[state_N];
1694 if (!SM)
1695 SM = getRegionStoreSubRegionMap(state_N->getStore());
1696 M = SM;
1697 }
1698
1699 RegionStoreSubRegionMap::iterator I, E;
1700 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1701 WorkList.push_back(std::make_pair(state_N, *I));
1702
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001703 // Enqueue the super region.
1704 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1705 const MemRegion *superR = SR->getSuperRegion();
1706 if (!isa<MemSpaceRegion>(superR)) {
1707 // If 'R' is a field or an element, we want to keep the bindings
1708 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001709 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001710 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1711 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001712 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001713 }
1714 }
1715
1716 // Mark the symbol for any live SymbolicRegion as "live". This means we
1717 // should continue to track that symbol.
1718 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1719 SymReaper.markLive(SymR->getSymbol());
1720
1721 Store store_N = state_N->getStore();
1722 RegionBindings B_N = GetRegionBindings(store_N);
1723
1724 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001725 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001726
Zhongxing Xu13d50172009-10-11 08:08:02 +00001727 if (V) {
1728 // Check for lazy bindings.
1729 if (const nonloc::LazyCompoundVal *LCV =
1730 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001731
Zhongxing Xu13d50172009-10-11 08:08:02 +00001732 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001733 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001734 }
1735 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001736 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001737 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001738 SI!=SE;++SI)
1739 SymReaper.markLive(*SI);
1740
Zhongxing Xu13d50172009-10-11 08:08:02 +00001741 // If V is a region, then add it to the worklist.
1742 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001743 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001744 }
1745 }
1746 }
1747
Ted Kremenek01756192009-10-29 05:14:17 +00001748 // See if any postponed SymbolicRegions are actually live now, after
1749 // having done a scan.
1750 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1751 E = Postponed.end() ; I != E ; ++I) {
1752 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1753 if (SymReaper.isLive(SR->getSymbol())) {
1754 WorkList.push_back(*I);
1755 I->second = NULL;
1756 }
1757 }
1758 }
1759
1760 if (!WorkList.empty())
1761 goto tryAgain;
1762
Ted Kremenek9af46f52009-06-16 22:36:44 +00001763 // We have now scanned the store, marking reachable regions and symbols
1764 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001765 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001766 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001767 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001768 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001769 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001770 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Ted Kremenek9af46f52009-06-16 22:36:44 +00001772 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001773 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Ted Kremenek9af46f52009-06-16 22:36:44 +00001775 // Mark all non-live symbols that this region references as dead.
1776 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1777 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Zhongxing Xu13d50172009-10-11 08:08:02 +00001779 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001780 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1781 for (; SI != SE; ++SI)
1782 SymReaper.maybeDead(*SI);
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001785 // Write the store back.
1786 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001787}
1788
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001789GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1790 StackFrameContext const *frame) {
1791 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1792 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1793
1794 FunctionDecl::param_const_iterator PI = FD->param_begin();
1795
1796 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1797
1798 // Copy the arg expression value to the arg variables.
1799 for (; AI != AE; ++AI, ++PI) {
1800 SVal ArgVal = state->getSVal(*AI);
1801 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1802 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1803 }
1804
1805 return state;
1806}
1807
Ted Kremenek9af46f52009-06-16 22:36:44 +00001808//===----------------------------------------------------------------------===//
1809// Utility methods.
1810//===----------------------------------------------------------------------===//
1811
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001812void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001813 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001814 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001815 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Ted Kremenek451ac092009-08-06 04:50:20 +00001817 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001818 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819}