blob: 9456ab64542cf6e2d1978f5f28bed8a0c9686e90 [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 {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167 Map::iterator I = M.find(Parent);
168
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,
265 const Expr *E, unsigned Count);
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000267private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000268 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000269 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
271public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000272 const GRState *Bind(const GRState *state, Loc LV, SVal V);
273
274 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000275 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000277 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
278 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000279
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000280 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
281 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000282 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000283 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000284
Ted Kremenek67f28532009-06-17 22:02:04 +0000285 /// BindStruct - Bind a compound value to a structure.
286 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Ted Kremenek67f28532009-06-17 22:02:04 +0000288 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
290 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000291 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000292
Ted Kremenek67f28532009-06-17 22:02:04 +0000293 Store Remove(Store store, Loc LV);
294
295 //===------------------------------------------------------------------===//
296 // Loading values from regions.
297 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Ted Kremenek67f28532009-06-17 22:02:04 +0000299 /// The high level logic for this method is this:
300 /// Retrieve (L)
301 /// if L has binding
302 /// return L's binding
303 /// else if L is in killset
304 /// return unknown
305 /// else
306 /// if L is on stack or heap
307 /// return undefined
308 /// else
309 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000310 SValuator::CastResult Retrieve(const GRState *state, Loc L,
311 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000312
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000313 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000314
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000315 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000317 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek9031dd72009-07-21 00:12:07 +0000319 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek25c54572009-07-20 22:58:02 +0000321 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000323 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
324 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek67f28532009-06-17 22:02:04 +0000326 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000327 /// struct copy:
328 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000329 /// x = y;
330 /// y's value is retrieved by this method.
331 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek67f28532009-06-17 22:02:04 +0000333 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000335 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000336 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000338 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
339 const GRState *state,
340 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000341
Ted Kremenek0954cde2009-09-24 04:11:44 +0000342 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
343 QualType T);
344
Ted Kremenek67f28532009-06-17 22:02:04 +0000345 //===------------------------------------------------------------------===//
346 // State pruning.
347 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
350 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000351 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
353
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000354 const GRState *EnterStackFrame(const GRState *state,
355 const StackFrameContext *frame);
356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 //===------------------------------------------------------------------===//
358 // Region "extents".
359 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
362 SVal getSizeInElements(const GRState *state, const MemRegion* R);
363
364 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000365 // Utility methods.
366 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek451ac092009-08-06 04:50:20 +0000368 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000369 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000370 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000371
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000372 void print(Store store, llvm::raw_ostream& Out, const char* nl,
373 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000374
375 void iterBindings(Store store, BindingsHandler& f) {
376 // FIXME: Implement.
377 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000378
Ted Kremenek67f28532009-06-17 22:02:04 +0000379 // FIXME: Remove.
380 BasicValueFactory& getBasicVals() {
381 return StateMgr.getBasicVals();
382 }
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek67f28532009-06-17 22:02:04 +0000384 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000385 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000386};
387
388} // end anonymous namespace
389
Ted Kremenek9af46f52009-06-16 22:36:44 +0000390//===----------------------------------------------------------------------===//
391// RegionStore creation.
392//===----------------------------------------------------------------------===//
393
394StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
395 RegionStoreFeatures F = maximal_features_tag();
396 return new RegionStoreManager(StMgr, F);
397}
398
399StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
400 RegionStoreFeatures F = minimal_features_tag();
401 F.enableFields(true);
402 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000403}
404
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000405void
406RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000407 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000408 const MemRegion *superR = R->getSuperRegion();
409 if (add(superR, R))
410 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000411 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000412}
413
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000414RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000415RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
416 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000417 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000419 llvm::SmallVector<const SubRegion*, 10> WL;
420
Ted Kremenek451ac092009-08-06 04:50:20 +0000421 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000422 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
423 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Mike Stump1eb44332009-09-09 15:08:12 +0000425 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000426 // don't have direct bindings but are super regions of those that do.
427 while (!WL.empty()) {
428 const SubRegion *R = WL.back();
429 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000431 }
432
Ted Kremenek14453bf2009-03-03 19:02:42 +0000433 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000434}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000435
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000436SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000437 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000438}
439
Ted Kremenek9af46f52009-06-16 22:36:44 +0000440//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000441// Binding invalidation.
442//===----------------------------------------------------------------------===//
443
Zhongxing Xu13d50172009-10-11 08:08:02 +0000444void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
445 const MemRegion *R,
446 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447 RegionStoreSubRegionMap::iterator I, E;
448
449 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000450 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000452 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000453}
454
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000455const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
456 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000457 const Expr *Ex,
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000458 unsigned Count) {
459 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000461 // Strip away casts.
462 R = R->getBaseRegion();
463
Ted Kremenek87806792009-09-27 20:45:21 +0000464 // Get the mapping of regions -> subregions.
465 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000466 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000467
468 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000469
Ted Kremenek87806792009-09-27 20:45:21 +0000470 llvm::DenseMap<const MemRegion *, unsigned> Visited;
471 llvm::SmallVector<const MemRegion *, 10> WorkList;
472 WorkList.push_back(R);
473
474 while (!WorkList.empty()) {
475 R = WorkList.back();
476 WorkList.pop_back();
477
478 // Have we visited this region before?
479 unsigned &visited = Visited[R];
480 if (visited)
481 continue;
482 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Ted Kremenek87806792009-09-27 20:45:21 +0000484 // Add subregions to work list.
485 RegionStoreSubRegionMap::iterator I, E;
486 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
487 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000488
489 // Get the old binding. Is it a region? If so, add it to the worklist.
490 if (Optional<SVal> V = getDirectBinding(B, R)) {
491 if (const MemRegion *RV = V->getAsRegion())
492 WorkList.push_back(RV);
493 }
494
Ted Kremenek87806792009-09-27 20:45:21 +0000495 // Handle region.
496 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
497 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000498 // Invalidate the region by setting its default value to
499 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000500 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
501 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000502 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000503 continue;
504 }
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Ted Kremenek87806792009-09-27 20:45:21 +0000506 if (!R->isBoundable())
507 continue;
508
509 const TypedRegion *TR = cast<TypedRegion>(R);
510 QualType T = TR->getValueType(Ctx);
511
512 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000513 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
514
Zhongxing Xu13d50172009-10-11 08:08:02 +0000515 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000516 if (!RD)
517 continue;
518
Zhongxing Xu13d50172009-10-11 08:08:02 +0000519 // Invalidate the region by setting its default value to
520 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000521 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
522 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000523 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000524 continue;
525 }
526
527 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
528 // Set the default value of the array to conjured symbol.
529 DefinedOrUnknownSVal V =
530 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000531 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000532 continue;
533 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000534
535 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
536 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000537 // For fields and elements whose super region has also been invalidated,
538 // only remove the old binding. The super region will get set with a
539 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000540 B = RBFactory.Remove(B, R);
541 continue;
542 }
Ted Kremenek87806792009-09-27 20:45:21 +0000543
Ted Kremenek389c44c2009-09-29 03:12:50 +0000544 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000545 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
546 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000547 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000548 }
549
Ted Kremenek87806792009-09-27 20:45:21 +0000550 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000551 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000552}
553
554//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000555// getLValueXXX methods.
556//===----------------------------------------------------------------------===//
557
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000558/// getLValueString - Returns an SVal representing the lvalue of a
559/// StringLiteral. Within RegionStore a StringLiteral has an
560/// associated StringRegion, and the lvalue of a StringLiteral is the
561/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000562SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000563 return loc::MemRegionVal(MRMgr.getStringRegion(S));
564}
565
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000566/// getLValueVar - Returns an SVal that represents the lvalue of a
567/// variable. Within RegionStore a variable has an associated
568/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000569SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000570 const LocationContext *LC) {
571 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000572}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000573
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000574/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
575/// of a compound literal. Within RegionStore a compound literal
576/// has an associated region, and the lvalue of the compound literal
577/// is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000578SVal
579RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000580 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
581}
582
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000583SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
584 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000585}
586
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000587SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
588 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000589}
590
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000591SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592 if (Base.isUnknownOrUndef())
593 return Base;
594
595 Loc BaseL = cast<Loc>(Base);
596 const MemRegion* BaseR = 0;
597
598 switch (BaseL.getSubKind()) {
599 case loc::MemRegionKind:
600 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
601 break;
602
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000603 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000604 // These are anormal cases. Flag an undefined value.
605 return UndefinedVal();
606
607 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000608 // While these seem funny, this can happen through casts.
609 // FIXME: What we should return is the field offset. For example,
610 // add the field offset to the integer value. That way funny things
611 // like this work properly: &(((struct foo *) 0xa)->f)
612 return Base;
613
614 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000615 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000616 return Base;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000619 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
620 // of FieldDecl.
621 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
622 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000623
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000624 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000625}
626
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000627SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
628 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000629
Ted Kremenekde7ec632009-03-09 22:44:49 +0000630 // If the base is an unknown or undefined value, just return it back.
631 // FIXME: For absolute pointer addresses, we just return that value back as
632 // well, although in reality we should return the offset added to that
633 // value.
634 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000635 return Base;
636
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000637 // Only handle integer offsets... for now.
638 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000639 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000640
Zhongxing Xuce760782009-05-09 13:20:07 +0000641 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000642
643 // Pointer of any type can be cast and used as array base.
644 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Ted Kremenek46537392009-07-16 01:33:37 +0000646 // Convert the offset to the appropriate size and signedness.
647 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000648
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000649 if (!ElemR) {
650 //
651 // If the base region is not an ElementRegion, create one.
652 // This can happen in the following example:
653 //
654 // char *p = __builtin_alloc(10);
655 // p[1] = 8;
656 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000657 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000658 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000659 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000660 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000663 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000665 if (!isa<nonloc::ConcreteInt>(BaseIdx))
666 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000668 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
669 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
670 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremenek46537392009-07-16 01:33:37 +0000672 // Compute the new index.
673 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremenek46537392009-07-16 01:33:37 +0000675 // Construct the new ElementRegion.
676 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000677 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000678 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000679}
680
Ted Kremenek9af46f52009-06-16 22:36:44 +0000681//===----------------------------------------------------------------------===//
682// Extents for regions.
683//===----------------------------------------------------------------------===//
684
Ted Kremenek67f28532009-06-17 22:02:04 +0000685SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000686 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000688 switch (R->getKind()) {
689 case MemRegion::MemSpaceRegionKind:
690 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000691 return UnknownVal();
692
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000693 case MemRegion::CodeTextRegionKind:
694 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000695 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000696
697 // Not yet handled.
698 case MemRegion::AllocaRegionKind:
699 case MemRegion::CompoundLiteralRegionKind:
700 case MemRegion::ElementRegionKind:
701 case MemRegion::FieldRegionKind:
702 case MemRegion::ObjCIvarRegionKind:
703 case MemRegion::ObjCObjectRegionKind:
704 case MemRegion::SymbolicRegionKind:
705 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000707 case MemRegion::StringRegionKind: {
708 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000709 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000710 // operations with signed indices.
711 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000714 case MemRegion::VarRegionKind: {
715 const VarRegion* VR = cast<VarRegion>(R);
716 // Get the type of the variable.
717 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000719 // FIXME: Handle variable-length arrays.
720 if (isa<VariableArrayType>(T))
721 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000723 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
724 // return the size as signed integer.
725 return ValMgr.makeIntVal(CAT->getSize(), false);
726 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000727
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000728 // Clients can use ordinary variables as if they were arrays. These
729 // essentially are arrays of size 1.
730 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000733 case MemRegion::BEG_DECL_REGIONS:
734 case MemRegion::END_DECL_REGIONS:
735 case MemRegion::BEG_TYPED_REGIONS:
736 case MemRegion::END_TYPED_REGIONS:
737 assert(0 && "Infeasible region");
738 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000739 }
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000741 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000742 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000743}
744
Ted Kremenek67f28532009-06-17 22:02:04 +0000745const GRState *RegionStoreManager::setExtent(const GRState *state,
746 const MemRegion *region,
747 SVal extent) {
748 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000749}
750
751//===----------------------------------------------------------------------===//
752// Location and region casting.
753//===----------------------------------------------------------------------===//
754
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000755/// ArrayToPointer - Emulates the "decay" of an array to a pointer
756/// type. 'Array' represents the lvalue of the array being decayed
757/// to a pointer, and the returned SVal represents the decayed
758/// version of that lvalue (i.e., a pointer to the first element of
759/// the array). This is called by GRExprEngine when evaluating casts
760/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000761SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000762 if (!isa<loc::MemRegionVal>(Array))
763 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenekabb042f2008-12-13 19:24:37 +0000765 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
766 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000768 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000769 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000771 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000772 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000773 ArrayType *AT = cast<ArrayType>(T);
774 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek75185b52009-07-16 00:00:11 +0000776 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
777 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000778
779 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000780}
781
Ted Kremenek9af46f52009-06-16 22:36:44 +0000782//===----------------------------------------------------------------------===//
783// Pointer arithmetic.
784//===----------------------------------------------------------------------===//
785
Mike Stump1eb44332009-09-09 15:08:12 +0000786SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000787 BinaryOperator::Opcode Op, Loc L, NonLoc R,
788 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000789 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000790 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000791 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000792
Zhongxing Xua1718c72009-04-03 07:33:13 +0000793 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000794 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000795
Ted Kremenek3bccf082009-07-11 00:58:27 +0000796 switch (MR->getKind()) {
797 case MemRegion::SymbolicRegionKind: {
798 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000799 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000800 QualType T = Sym->getType(getContext());
801 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000803 if (const PointerType *PT = T->getAs<PointerType>())
804 EleTy = PT->getPointeeType();
805 else
John McCall183700f2009-09-21 23:43:11 +0000806 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Ted Kremenek3bccf082009-07-11 00:58:27 +0000808 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
809 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000810 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000811 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000812 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000813 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000814 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000815 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000816 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
817 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000818 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000819 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000820
Ted Kremenek3bccf082009-07-11 00:58:27 +0000821 case MemRegion::ElementRegionKind: {
822 ER = cast<ElementRegion>(MR);
823 break;
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenek3bccf082009-07-11 00:58:27 +0000826 // Not yet handled.
827 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000828 case MemRegion::StringRegionKind: {
829
830 }
831 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000832 case MemRegion::CompoundLiteralRegionKind:
833 case MemRegion::FieldRegionKind:
834 case MemRegion::ObjCObjectRegionKind:
835 case MemRegion::ObjCIvarRegionKind:
836 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenek3bccf082009-07-11 00:58:27 +0000838 case MemRegion::CodeTextRegionKind:
839 // Technically this can happen if people do funny things with casts.
840 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek3bccf082009-07-11 00:58:27 +0000842 case MemRegion::MemSpaceRegionKind:
843 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
844 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek3bccf082009-07-11 00:58:27 +0000846 case MemRegion::BEG_DECL_REGIONS:
847 case MemRegion::END_DECL_REGIONS:
848 case MemRegion::BEG_TYPED_REGIONS:
849 case MemRegion::END_TYPED_REGIONS:
850 assert(0 && "Infeasible region");
851 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000852 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000853
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000854 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000855 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000856
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000857 // For now, only support:
858 // (a) concrete integer indices that can easily be resolved
859 // (b) 0 + symbolic index
860 if (Base) {
861 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
862 // FIXME: Should use SValuator here.
863 SVal NewIdx =
864 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000865 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000866 const MemRegion* NewER =
867 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
868 ER->getSuperRegion(), getContext());
869 return ValMgr.makeLoc(NewER);
870 }
871 if (0 == Base->getValue()) {
872 const MemRegion* NewER =
873 MRMgr.getElementRegion(ER->getElementType(), R,
874 ER->getSuperRegion(), getContext());
875 return ValMgr.makeLoc(NewER);
876 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek5dc27462009-03-03 02:51:43 +0000879 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000880}
881
Ted Kremenek9af46f52009-06-16 22:36:44 +0000882//===----------------------------------------------------------------------===//
883// Loading values from regions.
884//===----------------------------------------------------------------------===//
885
Zhongxing Xu13d50172009-10-11 08:08:02 +0000886Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
887 const MemRegion *R) {
888 if (const BindingVal *BV = B.lookup(R))
889 return Optional<SVal>::create(BV->getDirectValue());
890
891 return Optional<SVal>();
892}
893
894Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000895 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000897 if (R->isBoundable())
898 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
899 if (TR->getValueType(getContext())->isUnionType())
900 return UnknownVal();
901
Zhongxing Xu13d50172009-10-11 08:08:02 +0000902 if (BindingVal const *V = B.lookup(R))
903 return Optional<SVal>::create(V->getDefaultValue());
904
905 return Optional<SVal>();
906}
907
908Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
909 const MemRegion *R) {
910 if (const BindingVal *BV = B.lookup(R))
911 return Optional<SVal>::create(BV->getValue());
912
913 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000914}
915
Ted Kremeneka6275a52009-07-15 02:31:43 +0000916static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
917 RTy = Ctx.getCanonicalType(RTy);
918 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Ted Kremeneka6275a52009-07-15 02:31:43 +0000920 if (RTy == UsedTy)
921 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000922
923
Ted Kremenek25c54572009-07-20 22:58:02 +0000924 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000925 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000926 // represents a reinterpretation.
927 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000928 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000929 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000930
931 return PUsedTy && PRTy &&
932 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000933 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000934 }
935
936 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000937}
938
Ted Kremenek0954cde2009-09-24 04:11:44 +0000939const ElementRegion *
940RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
941 ASTContext &Ctx = getContext();
942 SVal idx = ValMgr.makeZeroArrayIndex();
943 assert(!T.isNull());
944 return MRMgr.getElementRegion(T, idx, SR, Ctx);
945}
946
947
948
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000949SValuator::CastResult
950RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000951
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000952 assert(!isa<UnknownVal>(L) && "location unknown");
953 assert(!isa<UndefinedVal>(L) && "location undefined");
954
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000955 // FIXME: Is this even possible? Shouldn't this be treated as a null
956 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000957 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000958 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000959
Ted Kremenek67f28532009-06-17 22:02:04 +0000960 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000961
Zhongxing Xu91844122009-05-20 09:18:48 +0000962 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000963 // Example:
964 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000965 // char* p = alloca();
966 // read(p);
967 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000968 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000969 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Ted Kremenek0954cde2009-09-24 04:11:44 +0000971 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
972 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Ted Kremenek968f0a62009-08-03 21:41:46 +0000974 if (isa<CodeTextRegion>(MR))
975 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000977 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
978 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000979 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000980 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000981
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000982 // FIXME: We should eventually handle funny addressing. e.g.:
983 //
984 // int x = ...;
985 // int *p = &x;
986 // char *q = (char*) p;
987 // char c = *q; // returns the first byte of 'x'.
988 //
989 // Such funny addressing will occur due to layering of regions.
990
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000991#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000992 ASTContext &Ctx = getContext();
993 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000994 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
995 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000996 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000997 assert(Ctx.getCanonicalType(RTy) ==
998 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +0000999 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001000#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001001
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001002 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001003 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001005 // FIXME: Handle unions.
1006 if (RTy->isUnionType())
1007 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001008
1009 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001010 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001011
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001012 // FIXME: handle Vector types.
1013 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001014 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001015
1016 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001017 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001018
1019 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001020 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Ted Kremenek25c54572009-07-20 22:58:02 +00001022 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001023 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Ted Kremenek9031dd72009-07-21 00:12:07 +00001025 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001026 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001027
Ted Kremenek451ac092009-08-06 04:50:20 +00001028 RegionBindings B = GetRegionBindings(state->getStore());
1029 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001030
1031 // Check if the region has a binding.
1032 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001033 if (SVal const *SV = V->getValue())
1034 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001035
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001036 // The location does not have a bound value. This means that it has
1037 // the value it had upon its creation and/or entry to the analyzed
1038 // function/method. These are either symbolic values or 'undefined'.
1039
Ted Kremenek356e9d62009-07-22 04:35:42 +00001040#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001041 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001042#else
1043 if (R->hasStackStorage()) {
1044#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001045 // All stack variables are considered to have undefined values
1046 // upon creation. All heap allocated blocks are considered to
1047 // have undefined values as well unless they are explicitly bound
1048 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001049 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001050 }
1051
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001052 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001053 return SValuator::CastResult(state,
1054 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001055}
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001057std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001058RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001059 if (Optional<SVal> OV = getDirectBinding(B, R))
1060 if (const nonloc::LazyCompoundVal *V =
1061 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1062 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001064 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1065 const std::pair<const GRState *, const MemRegion *> &X =
1066 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001068 if (X.first)
1069 return std::make_pair(X.first,
1070 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001071 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001072 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1073 const std::pair<const GRState *, const MemRegion *> &X =
1074 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001076 if (X.first)
1077 return std::make_pair(X.first,
1078 MRMgr.getFieldRegionWithSuper(FR, X.second));
1079 }
1080
1081 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1082}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001083
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001084SVal RegionStoreManager::RetrieveElement(const GRState* state,
1085 const ElementRegion* R) {
1086 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001087 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001088 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001089 return *V;
1090
Ted Kremenek921109a2009-07-01 23:19:52 +00001091 const MemRegion* superR = R->getSuperRegion();
1092
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001093 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001094 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001095 // FIXME: Handle loads from strings where the literal is treated as
1096 // an integer, e.g., *((unsigned int*)"hello")
1097 ASTContext &Ctx = getContext();
1098 QualType T = StrR->getValueType(Ctx)->getAs<ArrayType>()->getElementType();
1099 if (T != Ctx.getCanonicalType(R->getElementType()))
1100 return UnknownVal();
1101
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001102 const StringLiteral *Str = StrR->getStringLiteral();
1103 SVal Idx = R->getIndex();
1104 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1105 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001106 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001107 if (i > byteLength) {
1108 // Buffer overflow checking in GRExprEngine should handle this case,
1109 // but we shouldn't rely on it to not overflow here if that checking
1110 // is disabled.
1111 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001112 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001113 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001114 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001115 }
1116 }
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001118 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001119 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001120 if (SymbolRef parentSym = V->getAsSymbol())
1121 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001122
1123 if (V->isUnknownOrUndef())
1124 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001125
1126 // Handle LazyCompoundVals for the immediate super region. Other cases
1127 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001128 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001129 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001131 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1132 return RetrieveElement(LCV->getState(), R);
1133 }
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremeneka6275a52009-07-15 02:31:43 +00001135 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001136 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001137 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001138
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001139 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001140}
1141
Mike Stump1eb44332009-09-09 15:08:12 +00001142SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001143 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001144
1145 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001146 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001147 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001148 return *V;
1149
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001150 QualType Ty = R->getValueType(getContext());
1151 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1152}
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001154SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1155 const TypedRegion *R,
1156 QualType Ty,
1157 const MemRegion *superR) {
1158
Mike Stump1eb44332009-09-09 15:08:12 +00001159 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001160 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001162 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001164 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001165 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001166 if (SymbolRef parentSym = D->getAsSymbol())
1167 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001169 if (D->isZeroConstant())
1170 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001172 if (D->isUnknown())
1173 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001175 assert(0 && "Unknown default value");
1176 }
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001178 // If our super region is a field or element itself, walk up the region
1179 // hierarchy to see if there is a default value installed in an ancestor.
1180 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1181 superR = cast<SubRegion>(superR)->getSuperRegion();
1182 continue;
1183 }
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001185 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001188 // Lazy binding?
1189 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001190 const MemRegion *lazyBindingRegion = NULL;
1191 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001193 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001194 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001196 if (isa<ElementRegion>(R))
1197 return RetrieveElement(lazyBindingState,
1198 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001200 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001201 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001202 }
1203
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001204 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001206 if (isa<ElementRegion>(R)) {
1207 // Currently we don't reason specially about Clang-style vectors. Check
1208 // if superR is a vector and if so return Unknown.
1209 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1210 if (typedSuperR->getValueType(getContext())->isVectorType())
1211 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001212 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001215 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001218 // All other values are symbolic.
1219 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001220}
Mike Stump1eb44332009-09-09 15:08:12 +00001221
1222SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001223 const ObjCIvarRegion* R) {
1224
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001225 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001226 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001227
Zhongxing Xu13d50172009-10-11 08:08:02 +00001228 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001229 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001231 const MemRegion *superR = R->getSuperRegion();
1232
1233 // Check if the super region has a binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001234 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001235 if (SymbolRef parentSym = V->getAsSymbol())
1236 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001238 // Other cases: give up.
1239 return UnknownVal();
1240 }
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Ted Kremenek25c54572009-07-20 22:58:02 +00001242 return RetrieveLazySymbol(state, R);
1243}
1244
Ted Kremenek9031dd72009-07-21 00:12:07 +00001245SVal RegionStoreManager::RetrieveVar(const GRState *state,
1246 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenek9031dd72009-07-21 00:12:07 +00001248 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001249 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Zhongxing Xu13d50172009-10-11 08:08:02 +00001251 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001252 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek9031dd72009-07-21 00:12:07 +00001254 // Lazily derive a value for the VarRegion.
1255 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek9031dd72009-07-21 00:12:07 +00001257 if (R->hasGlobalsOrParametersStorage())
1258 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenek9031dd72009-07-21 00:12:07 +00001260 return UndefinedVal();
1261}
1262
Mike Stump1eb44332009-09-09 15:08:12 +00001263SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001264 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Ted Kremenek25c54572009-07-20 22:58:02 +00001266 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001267
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001268 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001269 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001270}
1271
Mike Stump1eb44332009-09-09 15:08:12 +00001272SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1273 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001274 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001275 assert(T->isStructureType());
1276
Zhongxing Xub7507d12009-06-11 07:27:30 +00001277 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001278 RecordDecl* RD = RT->getDecl();
1279 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001280 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001281#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001282 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1283
Ted Kremenek67f28532009-06-17 22:02:04 +00001284 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1285 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001286 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001287
Douglas Gregore267ff32008-12-11 20:41:00 +00001288 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1289 FieldEnd = Fields.rend();
1290 Field != FieldEnd; ++Field) {
1291 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001292 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001293 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001294 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1295 }
1296
Zhongxing Xud91ee272009-06-23 09:02:15 +00001297 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001298#else
1299 return ValMgr.makeLazyCompoundVal(state, R);
1300#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001301}
1302
Ted Kremenek67f28532009-06-17 22:02:04 +00001303SVal RegionStoreManager::RetrieveArray(const GRState *state,
1304 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001305#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001306 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001307 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1308
1309 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001310 uint64_t size = CAT->getSize().getZExtValue();
1311 for (uint64_t i = 0; i < size; ++i) {
1312 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001313 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001314 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001315 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001316 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001317 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1318 }
1319
Zhongxing Xud91ee272009-06-23 09:02:15 +00001320 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001321#else
1322 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1323 return ValMgr.makeLazyCompoundVal(state, R);
1324#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001325}
1326
Ted Kremenek9af46f52009-06-16 22:36:44 +00001327//===----------------------------------------------------------------------===//
1328// Binding values to regions.
1329//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001330
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001331Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001332 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Ted Kremenek0964a062009-01-21 06:57:53 +00001334 if (isa<loc::MemRegionVal>(L))
1335 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Ted Kremenek0964a062009-01-21 06:57:53 +00001337 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001338 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001339 return RBFactory.Remove(B, R).getRoot();
1340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Ted Kremenek0964a062009-01-21 06:57:53 +00001342 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001343}
1344
Ted Kremenek67f28532009-06-17 22:02:04 +00001345const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001346 if (isa<loc::ConcreteInt>(L))
1347 return state;
1348
Ted Kremenek9af46f52009-06-16 22:36:44 +00001349 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001350 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Ted Kremenek9af46f52009-06-16 22:36:44 +00001352 // Check if the region is a struct region.
1353 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1354 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001355 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001357 // Special case: the current region represents a cast and it and the super
1358 // region both have pointer types or intptr_t types. If so, perform the
1359 // bind to the super region.
1360 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001362 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1363 if (ER->getIndex().isZeroConstant()) {
1364 if (const TypedRegion *superR =
1365 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1366 ASTContext &Ctx = getContext();
1367 QualType superTy = superR->getValueType(Ctx);
1368 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001369
1370 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001371 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001372 SValuator::CastResult cr =
1373 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001374 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1375 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001376 // For now, just invalidate the fields of the struct/union/class.
1377 // FIXME: Precisely handle the fields of the record.
1378 if (superTy->isRecordType())
1379 return InvalidateRegion(state, superR, NULL, 0);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001380 }
1381 }
1382 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001383 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1384 // Binding directly to a symbolic region should be treated as binding
1385 // to element 0.
1386 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001387 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001388 R = GetElementZeroRegion(SR, T);
1389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001391 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001392 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001393 return state->makeWithStore(
1394 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001395}
1396
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001397const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1398 const VarDecl *VD,
1399 const LocationContext *LC,
1400 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001401
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001402 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001403 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001404
Ted Kremenek0964a062009-01-21 06:57:53 +00001405 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001406 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001407 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001408 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001409
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001410 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001411}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001412
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001413// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001414const GRState *
1415RegionStoreManager::BindCompoundLiteral(const GRState *state,
1416 const CompoundLiteralExpr* CL,
1417 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001419 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001420 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001421}
1422
Ted Kremenek67f28532009-06-17 22:02:04 +00001423const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001424 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001425 SVal Init) {
1426
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001427 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001428 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001429 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001430
Ted Kremenek46537392009-07-16 01:33:37 +00001431 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001432
1433 // Check if the init expr is a StringLiteral.
1434 if (isa<loc::MemRegionVal>(Init)) {
1435 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1436 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1437 const char* str = S->getStrData();
1438 unsigned len = S->getByteLength();
1439 unsigned j = 0;
1440
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001441 // Copy bytes from the string literal into the target array. Trailing bytes
1442 // in the array that are not covered by the string literal are initialized
1443 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001444 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001445 if (j >= len)
1446 break;
1447
Ted Kremenek46537392009-07-16 01:33:37 +00001448 SVal Idx = ValMgr.makeArrayIndex(i);
1449 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1450 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001451
Zhongxing Xud91ee272009-06-23 09:02:15 +00001452 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001453 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001454 }
1455
Ted Kremenek67f28532009-06-17 22:02:04 +00001456 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001457 }
1458
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001459 // Handle lazy compound values.
1460 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1461 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
1463 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001464 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001465 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001466 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Ted Kremenek46537392009-07-16 01:33:37 +00001468 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001469 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001470 if (VI == VE)
1471 break;
1472
Ted Kremenek46537392009-07-16 01:33:37 +00001473 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001474 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001475
1476 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001477 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001478 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001479 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001480 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001481 }
1482
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001483 // If the init list is shorter than the array length, set the array default
1484 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001485 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001486 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001487 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001488 Store store = state->getStore();
1489 RegionBindings B = GetRegionBindings(store);
1490 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1491 state = state->makeWithStore(B.getRoot());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001492 }
1493 }
1494
Ted Kremenek67f28532009-06-17 22:02:04 +00001495 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001496}
1497
Ted Kremenek67f28532009-06-17 22:02:04 +00001498const GRState *
1499RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1500 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Ted Kremenek67f28532009-06-17 22:02:04 +00001502 if (!Features.supportsFields())
1503 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001505 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001506 assert(T->isStructureType());
1507
Ted Kremenek6217b802009-07-29 21:53:49 +00001508 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001509 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001510
1511 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001512 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001513
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001514 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001515 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001516 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Ted Kremenek67f28532009-06-17 22:02:04 +00001518 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1519 // Ignore them and kill the field values.
1520 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001521 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001522
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001523 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001524 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001525
1526 RecordDecl::field_iterator FI, FE;
1527
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001528 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001529
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001530 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001531 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001532
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001533 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001534 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001535
Ted Kremenekcf549592009-09-22 21:19:14 +00001536 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001537 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001538 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001539 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001540 else
1541 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001542 }
1543
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001544 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001545 if (FI != FE) {
1546 Store store = state->getStore();
1547 RegionBindings B = GetRegionBindings(store);
1548 B = RBFactory.Add(B, R,
1549 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1550 state = state->makeWithStore(B.getRoot());
1551 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001552
Ted Kremenek67f28532009-06-17 22:02:04 +00001553 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001554}
1555
Zhongxing Xu13d50172009-10-11 08:08:02 +00001556Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1557 RegionBindings B = GetRegionBindings(store);
1558 llvm::OwningPtr<RegionStoreSubRegionMap>
1559 SubRegions(getRegionStoreSubRegionMap(store));
1560 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001561
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001562 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001563 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001564
Zhongxing Xu13d50172009-10-11 08:08:02 +00001565 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001566}
1567
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001568const GRState*
1569RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1570 const GRState *state,
1571 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001572
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001573 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001574 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001575
Mike Stump1eb44332009-09-09 15:08:12 +00001576 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001577 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001578
Mike Stump1eb44332009-09-09 15:08:12 +00001579 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001580 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001582 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1583 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001584 return state->makeWithStore(
1585 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001586}
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Ted Kremenek9af46f52009-06-16 22:36:44 +00001588//===----------------------------------------------------------------------===//
1589// State pruning.
1590//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001591
Ted Kremenek451ac092009-08-06 04:50:20 +00001592namespace {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001593class VISIBILITY_HIDDEN RBDNode
1594 : public std::pair<const GRState*, const MemRegion *> {
Ted Kremenek451ac092009-08-06 04:50:20 +00001595public:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001596 RBDNode(const GRState *st, const MemRegion *r)
1597 : std::pair<const GRState*, const MemRegion*>(st, r) {}
1598
1599 const GRState *getState() const { return first; }
1600 const MemRegion *getRegion() const { return second; }
1601};
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001603enum VisitFlag { NotVisited = 0, VisitedFromSubRegion, VisitedFromSuperRegion };
1604
1605class RBDItem : public RBDNode {
1606private:
1607 const VisitFlag VF;
1608
1609public:
1610 RBDItem(const GRState *st, const MemRegion *r, VisitFlag vf)
1611 : RBDNode(st, r), VF(vf) {}
1612
1613 VisitFlag getVisitFlag() const { return VF; }
Ted Kremenek451ac092009-08-06 04:50:20 +00001614};
1615} // end anonymous namespace
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001616
Mike Stump1eb44332009-09-09 15:08:12 +00001617void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001618 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001619 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001620{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001621 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001622 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Ted Kremenek9af46f52009-06-16 22:36:44 +00001624 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001625 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001626 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001627
1628 // Do a pass over the regions in the store. For VarRegions we check if
1629 // the variable is still live and if so add it to the list of live roots.
1630 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001631 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001632
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001633 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001634 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001635 const MemRegion *R = I.getKey();
1636 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001637 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001638
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001639 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001640 // real roots.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001641 llvm::SmallVector<RBDItem, 10> WorkList;
1642 llvm::DenseMap<const MemRegion*,unsigned> IntermediateVisited;
1643
Ted Kremenek9af46f52009-06-16 22:36:44 +00001644 while (!IntermediateRoots.empty()) {
1645 const MemRegion* R = IntermediateRoots.back();
1646 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001647
1648 unsigned &visited = IntermediateVisited[R];
1649 if (visited)
1650 continue;
1651 visited = 1;
1652
Ted Kremenek9af46f52009-06-16 22:36:44 +00001653 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001654 if (SymReaper.isLive(Loc, VR->getDecl()))
1655 WorkList.push_back(RBDItem(&state, VR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001656 continue;
1657 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001658
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001659 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001660 if (SymReaper.isLive(SR->getSymbol()))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001661 WorkList.push_back(RBDItem(&state, SR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001662 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001663 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001664
1665 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001666 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001667 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001668 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 }
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001671 // Enqueue the RegionRoots onto WorkList.
1672 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1673 E=RegionRoots.end(); I!=E; ++I) {
1674 WorkList.push_back(RBDItem(&state, *I, VisitedFromSuperRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001675 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001676 RegionRoots.clear();
1677
1678 // Process the worklist.
1679 typedef llvm::DenseMap<std::pair<const GRState*, const MemRegion*>, VisitFlag>
1680 VisitMap;
1681
1682 VisitMap Visited;
1683
1684 while (!WorkList.empty()) {
1685 RBDItem N = WorkList.back();
1686 WorkList.pop_back();
1687
1688 // Have we visited this node before?
1689 VisitFlag &VF = Visited[N];
1690 if (VF >= N.getVisitFlag())
1691 continue;
1692
1693 const MemRegion *R = N.getRegion();
1694 const GRState *state_N = N.getState();
1695
1696 // Enqueue subregions?
1697 if (N.getVisitFlag() == VisitedFromSuperRegion) {
1698 RegionStoreSubRegionMap *M;
1699
1700 if (&state == state_N)
1701 M = SubRegions.get();
1702 else {
1703 RegionStoreSubRegionMap *& SM = SC[state_N];
1704 if (!SM)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001705 SM = getRegionStoreSubRegionMap(state_N->getStore());
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001706 M = SM;
1707 }
1708
1709 RegionStoreSubRegionMap::iterator I, E;
1710 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1711 WorkList.push_back(RBDItem(state_N, *I, VisitedFromSuperRegion));
1712 }
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001714 // At this point, if we have already visited this region before, we are
1715 // done.
1716 if (VF != NotVisited) {
1717 VF = N.getVisitFlag();
1718 continue;
1719 }
1720 VF = N.getVisitFlag();
1721
1722 // Enqueue the super region.
1723 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1724 const MemRegion *superR = SR->getSuperRegion();
1725 if (!isa<MemSpaceRegion>(superR)) {
1726 // If 'R' is a field or an element, we want to keep the bindings
1727 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001728 // pointer arithmetic can get us to the other fields or elements.
1729 // FIXME: add an assertion that this is always true.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001730 VisitFlag NewVisit =
1731 isa<FieldRegion>(R) || isa<ElementRegion>(R) || isa<ObjCIvarRegion>(R)
1732 ? VisitedFromSuperRegion : VisitedFromSubRegion;
1733
1734 WorkList.push_back(RBDItem(state_N, superR, NewVisit));
1735 }
1736 }
1737
1738 // Mark the symbol for any live SymbolicRegion as "live". This means we
1739 // should continue to track that symbol.
1740 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1741 SymReaper.markLive(SymR->getSymbol());
1742
1743 Store store_N = state_N->getStore();
1744 RegionBindings B_N = GetRegionBindings(store_N);
1745
1746 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001747 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001748
Zhongxing Xu13d50172009-10-11 08:08:02 +00001749 if (V) {
1750 // Check for lazy bindings.
1751 if (const nonloc::LazyCompoundVal *LCV =
1752 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001753
Zhongxing Xu13d50172009-10-11 08:08:02 +00001754 const LazyCompoundValData *D = LCV->getCVData();
1755 WorkList.push_back(RBDItem(D->getState(), D->getRegion(),
1756 VisitedFromSuperRegion));
1757 }
1758 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001759 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001760 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001761 SI!=SE;++SI)
1762 SymReaper.markLive(*SI);
1763
Zhongxing Xu13d50172009-10-11 08:08:02 +00001764 // If V is a region, then add it to the worklist.
1765 if (const MemRegion *RX = V->getAsRegion())
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001766 WorkList.push_back(RBDItem(state_N, RX, VisitedFromSuperRegion));
1767 }
1768 }
1769 }
1770
Ted Kremenek9af46f52009-06-16 22:36:44 +00001771 // We have now scanned the store, marking reachable regions and symbols
1772 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001773 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001774 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001775 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001776 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001777 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek9af46f52009-06-16 22:36:44 +00001778 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Ted Kremenek9af46f52009-06-16 22:36:44 +00001780 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001781 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Ted Kremenek9af46f52009-06-16 22:36:44 +00001783 // Mark all non-live symbols that this region references as dead.
1784 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1785 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Zhongxing Xu13d50172009-10-11 08:08:02 +00001787 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001788 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1789 for (; SI != SE; ++SI)
1790 SymReaper.maybeDead(*SI);
1791 }
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001793 // Write the store back.
1794 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001795}
1796
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001797GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1798 StackFrameContext const *frame) {
1799 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1800 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1801
1802 FunctionDecl::param_const_iterator PI = FD->param_begin();
1803
1804 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1805
1806 // Copy the arg expression value to the arg variables.
1807 for (; AI != AE; ++AI, ++PI) {
1808 SVal ArgVal = state->getSVal(*AI);
1809 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1810 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1811 }
1812
1813 return state;
1814}
1815
Ted Kremenek9af46f52009-06-16 22:36:44 +00001816//===----------------------------------------------------------------------===//
1817// Utility methods.
1818//===----------------------------------------------------------------------===//
1819
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001820void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001821 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001822 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001823 OS << "Store (direct bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Ted Kremenek451ac092009-08-06 04:50:20 +00001825 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001826 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001827}