blob: f0bf072fa4fdf19ed8177eb75b04a8efc4d9c317 [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.
Ted Kremenek67f28532009-06-17 22:02:04 +0000223 SVal getLValueString(const GRState *state, 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.
Ted Kremenek67f28532009-06-17 22:02:04 +0000229 SVal getLValueCompoundLiteral(const GRState *state, 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.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000234 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
235 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenek67f28532009-06-17 22:02:04 +0000237 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000238
Ted Kremenek67f28532009-06-17 22:02:04 +0000239 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenek67f28532009-06-17 22:02:04 +0000241 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000242
Ted Kremenek67f28532009-06-17 22:02:04 +0000243 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000244 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000245
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000246
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000247 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
248 /// type. 'Array' represents the lvalue of the array being decayed
249 /// to a pointer, and the returned SVal represents the decayed
250 /// version of that lvalue (i.e., a pointer to the first element of
251 /// the array). This is called by GRExprEngine when evaluating
252 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000253 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000254
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000255 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000256 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000257
Mike Stump1eb44332009-09-09 15:08:12 +0000258 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000259 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000260 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000261
Ted Kremenek67f28532009-06-17 22:02:04 +0000262 //===-------------------------------------------------------------------===//
263 // Binding values to regions.
264 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000265
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000266 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
267 const Expr *E, unsigned Count);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000269private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000270 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000271 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
273public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000274 const GRState *Bind(const GRState *state, Loc LV, SVal V);
275
276 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000277 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000279 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
280 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000281
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000282 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
283 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000284 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000285 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000286
Ted Kremenek67f28532009-06-17 22:02:04 +0000287 /// BindStruct - Bind a compound value to a structure.
288 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenek67f28532009-06-17 22:02:04 +0000290 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
292 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000293 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000294
Ted Kremenek67f28532009-06-17 22:02:04 +0000295 Store Remove(Store store, Loc LV);
296
297 //===------------------------------------------------------------------===//
298 // Loading values from regions.
299 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenek67f28532009-06-17 22:02:04 +0000301 /// The high level logic for this method is this:
302 /// Retrieve (L)
303 /// if L has binding
304 /// return L's binding
305 /// else if L is in killset
306 /// return unknown
307 /// else
308 /// if L is on stack or heap
309 /// return undefined
310 /// else
311 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000312 SValuator::CastResult Retrieve(const GRState *state, Loc L,
313 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000314
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000315 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000316
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000317 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000319 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek9031dd72009-07-21 00:12:07 +0000321 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek25c54572009-07-20 22:58:02 +0000323 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000325 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
326 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek67f28532009-06-17 22:02:04 +0000328 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000329 /// struct copy:
330 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000331 /// x = y;
332 /// y's value is retrieved by this method.
333 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek67f28532009-06-17 22:02:04 +0000335 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000337 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000338 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000340 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
341 const GRState *state,
342 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000343
Ted Kremenek0954cde2009-09-24 04:11:44 +0000344 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
345 QualType T);
346
Ted Kremenek67f28532009-06-17 22:02:04 +0000347 //===------------------------------------------------------------------===//
348 // State pruning.
349 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek67f28532009-06-17 22:02:04 +0000351 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
352 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000353 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000354 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
355
356 //===------------------------------------------------------------------===//
357 // Region "extents".
358 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
361 SVal getSizeInElements(const GRState *state, const MemRegion* R);
362
363 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000364 // Utility methods.
365 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Ted Kremenek451ac092009-08-06 04:50:20 +0000367 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000368 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000369 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000370
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000371 void print(Store store, llvm::raw_ostream& Out, const char* nl,
372 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000373
374 void iterBindings(Store store, BindingsHandler& f) {
375 // FIXME: Implement.
376 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000377
Ted Kremenek67f28532009-06-17 22:02:04 +0000378 // FIXME: Remove.
379 BasicValueFactory& getBasicVals() {
380 return StateMgr.getBasicVals();
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Ted Kremenek67f28532009-06-17 22:02:04 +0000383 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000384 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000385};
386
387} // end anonymous namespace
388
Ted Kremenek9af46f52009-06-16 22:36:44 +0000389//===----------------------------------------------------------------------===//
390// RegionStore creation.
391//===----------------------------------------------------------------------===//
392
393StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
394 RegionStoreFeatures F = maximal_features_tag();
395 return new RegionStoreManager(StMgr, F);
396}
397
398StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
399 RegionStoreFeatures F = minimal_features_tag();
400 F.enableFields(true);
401 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000402}
403
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000404void
405RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000406 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000407 const MemRegion *superR = R->getSuperRegion();
408 if (add(superR, R))
409 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000410 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000411}
412
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000413RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000414RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
415 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000416 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000418 llvm::SmallVector<const SubRegion*, 10> WL;
419
Ted Kremenek451ac092009-08-06 04:50:20 +0000420 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000421 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
422 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Mike Stump1eb44332009-09-09 15:08:12 +0000424 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000425 // don't have direct bindings but are super regions of those that do.
426 while (!WL.empty()) {
427 const SubRegion *R = WL.back();
428 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000429 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000430 }
431
Ted Kremenek14453bf2009-03-03 19:02:42 +0000432 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000433}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000434
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000435SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000436 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437}
438
Ted Kremenek9af46f52009-06-16 22:36:44 +0000439//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000440// Binding invalidation.
441//===----------------------------------------------------------------------===//
442
Zhongxing Xu13d50172009-10-11 08:08:02 +0000443void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
444 const MemRegion *R,
445 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000446 RegionStoreSubRegionMap::iterator I, E;
447
448 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000449 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000451 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000452}
453
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000454const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
455 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000456 const Expr *Ex,
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000457 unsigned Count) {
458 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000460 // Strip away casts.
461 R = R->getBaseRegion();
462
Ted Kremenek87806792009-09-27 20:45:21 +0000463 // Get the mapping of regions -> subregions.
464 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000465 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000466
467 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000468
Ted Kremenek87806792009-09-27 20:45:21 +0000469 llvm::DenseMap<const MemRegion *, unsigned> Visited;
470 llvm::SmallVector<const MemRegion *, 10> WorkList;
471 WorkList.push_back(R);
472
473 while (!WorkList.empty()) {
474 R = WorkList.back();
475 WorkList.pop_back();
476
477 // Have we visited this region before?
478 unsigned &visited = Visited[R];
479 if (visited)
480 continue;
481 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Ted Kremenek87806792009-09-27 20:45:21 +0000483 // Add subregions to work list.
484 RegionStoreSubRegionMap::iterator I, E;
485 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
486 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000487
488 // Get the old binding. Is it a region? If so, add it to the worklist.
489 if (Optional<SVal> V = getDirectBinding(B, R)) {
490 if (const MemRegion *RV = V->getAsRegion())
491 WorkList.push_back(RV);
492 }
493
Ted Kremenek87806792009-09-27 20:45:21 +0000494 // Handle region.
495 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
496 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000497 // Invalidate the region by setting its default value to
498 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000499 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
500 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000501 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000502 continue;
503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek87806792009-09-27 20:45:21 +0000505 if (!R->isBoundable())
506 continue;
507
508 const TypedRegion *TR = cast<TypedRegion>(R);
509 QualType T = TR->getValueType(Ctx);
510
511 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000512 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
513
Zhongxing Xu13d50172009-10-11 08:08:02 +0000514 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000515 if (!RD)
516 continue;
517
Zhongxing Xu13d50172009-10-11 08:08:02 +0000518 // Invalidate the region by setting its default value to
519 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000520 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
521 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000522 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000523 continue;
524 }
525
526 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
527 // Set the default value of the array to conjured symbol.
528 DefinedOrUnknownSVal V =
529 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000530 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000531 continue;
532 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000533
534 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
535 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000536 // For fields and elements whose super region has also been invalidated,
537 // only remove the old binding. The super region will get set with a
538 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000539 B = RBFactory.Remove(B, R);
540 continue;
541 }
Ted Kremenek87806792009-09-27 20:45:21 +0000542
Ted Kremenek389c44c2009-09-29 03:12:50 +0000543 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000544 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
545 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000546 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000547 }
548
Ted Kremenek87806792009-09-27 20:45:21 +0000549 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000550 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000551}
552
553//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000554// getLValueXXX methods.
555//===----------------------------------------------------------------------===//
556
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000557/// getLValueString - Returns an SVal representing the lvalue of a
558/// StringLiteral. Within RegionStore a StringLiteral has an
559/// associated StringRegion, and the lvalue of a StringLiteral is the
560/// lvalue of that region.
Mike Stump1eb44332009-09-09 15:08:12 +0000561SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000562 const StringLiteral* S) {
563 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.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000569SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
570 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 Xu13d50172009-10-11 08:08:02 +0000578SVal RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
579 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000580 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
581}
582
Ted Kremenek67f28532009-06-17 22:02:04 +0000583SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000584 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000585 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000586}
587
Ted Kremenek67f28532009-06-17 22:02:04 +0000588SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000589 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000590 return getLValueFieldOrIvar(St, Base, D);
591}
592
Ted Kremenek67f28532009-06-17 22:02:04 +0000593SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000594 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000595 if (Base.isUnknownOrUndef())
596 return Base;
597
598 Loc BaseL = cast<Loc>(Base);
599 const MemRegion* BaseR = 0;
600
601 switch (BaseL.getSubKind()) {
602 case loc::MemRegionKind:
603 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
604 break;
605
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000606 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000607 // These are anormal cases. Flag an undefined value.
608 return UndefinedVal();
609
610 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000611 // While these seem funny, this can happen through casts.
612 // FIXME: What we should return is the field offset. For example,
613 // add the field offset to the integer value. That way funny things
614 // like this work properly: &(((struct foo *) 0xa)->f)
615 return Base;
616
617 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000618 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000619 return Base;
620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000622 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
623 // of FieldDecl.
624 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
625 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000626
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000627 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000628}
629
Ted Kremenek67f28532009-06-17 22:02:04 +0000630SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000631 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000632 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000633
Ted Kremenekde7ec632009-03-09 22:44:49 +0000634 // If the base is an unknown or undefined value, just return it back.
635 // FIXME: For absolute pointer addresses, we just return that value back as
636 // well, although in reality we should return the offset added to that
637 // value.
638 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000639 return Base;
640
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000641 // Only handle integer offsets... for now.
642 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000643 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000644
Zhongxing Xuce760782009-05-09 13:20:07 +0000645 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000646
647 // Pointer of any type can be cast and used as array base.
648 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremenek46537392009-07-16 01:33:37 +0000650 // Convert the offset to the appropriate size and signedness.
651 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000653 if (!ElemR) {
654 //
655 // If the base region is not an ElementRegion, create one.
656 // This can happen in the following example:
657 //
658 // char *p = __builtin_alloc(10);
659 // p[1] = 8;
660 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000661 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000662 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000663 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000664 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000665 }
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000667 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000669 if (!isa<nonloc::ConcreteInt>(BaseIdx))
670 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000672 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
673 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
674 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek46537392009-07-16 01:33:37 +0000676 // Compute the new index.
677 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek46537392009-07-16 01:33:37 +0000679 // Construct the new ElementRegion.
680 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000681 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000682 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000683}
684
Ted Kremenek9af46f52009-06-16 22:36:44 +0000685//===----------------------------------------------------------------------===//
686// Extents for regions.
687//===----------------------------------------------------------------------===//
688
Ted Kremenek67f28532009-06-17 22:02:04 +0000689SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000690 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000692 switch (R->getKind()) {
693 case MemRegion::MemSpaceRegionKind:
694 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000695 return UnknownVal();
696
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000697 case MemRegion::CodeTextRegionKind:
698 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000699 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000700
701 // Not yet handled.
702 case MemRegion::AllocaRegionKind:
703 case MemRegion::CompoundLiteralRegionKind:
704 case MemRegion::ElementRegionKind:
705 case MemRegion::FieldRegionKind:
706 case MemRegion::ObjCIvarRegionKind:
707 case MemRegion::ObjCObjectRegionKind:
708 case MemRegion::SymbolicRegionKind:
709 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000711 case MemRegion::StringRegionKind: {
712 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000713 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000714 // operations with signed indices.
715 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000716 }
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000718 case MemRegion::VarRegionKind: {
719 const VarRegion* VR = cast<VarRegion>(R);
720 // Get the type of the variable.
721 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000723 // FIXME: Handle variable-length arrays.
724 if (isa<VariableArrayType>(T))
725 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000727 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
728 // return the size as signed integer.
729 return ValMgr.makeIntVal(CAT->getSize(), false);
730 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000731
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000732 // Clients can use ordinary variables as if they were arrays. These
733 // essentially are arrays of size 1.
734 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000737 case MemRegion::BEG_DECL_REGIONS:
738 case MemRegion::END_DECL_REGIONS:
739 case MemRegion::BEG_TYPED_REGIONS:
740 case MemRegion::END_TYPED_REGIONS:
741 assert(0 && "Infeasible region");
742 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000743 }
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000745 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000746 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000747}
748
Ted Kremenek67f28532009-06-17 22:02:04 +0000749const GRState *RegionStoreManager::setExtent(const GRState *state,
750 const MemRegion *region,
751 SVal extent) {
752 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000753}
754
755//===----------------------------------------------------------------------===//
756// Location and region casting.
757//===----------------------------------------------------------------------===//
758
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000759/// ArrayToPointer - Emulates the "decay" of an array to a pointer
760/// type. 'Array' represents the lvalue of the array being decayed
761/// to a pointer, and the returned SVal represents the decayed
762/// version of that lvalue (i.e., a pointer to the first element of
763/// the array). This is called by GRExprEngine when evaluating casts
764/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000765SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000766 if (!isa<loc::MemRegionVal>(Array))
767 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenekabb042f2008-12-13 19:24:37 +0000769 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
770 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000772 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000773 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000775 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000776 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000777 ArrayType *AT = cast<ArrayType>(T);
778 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Ted Kremenek75185b52009-07-16 00:00:11 +0000780 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
781 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000782
783 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000784}
785
Ted Kremenek9af46f52009-06-16 22:36:44 +0000786//===----------------------------------------------------------------------===//
787// Pointer arithmetic.
788//===----------------------------------------------------------------------===//
789
Mike Stump1eb44332009-09-09 15:08:12 +0000790SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000791 BinaryOperator::Opcode Op, Loc L, NonLoc R,
792 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000793 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000794 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000795 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000796
Zhongxing Xua1718c72009-04-03 07:33:13 +0000797 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000798 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000799
Ted Kremenek3bccf082009-07-11 00:58:27 +0000800 switch (MR->getKind()) {
801 case MemRegion::SymbolicRegionKind: {
802 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000803 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000804 QualType T = Sym->getType(getContext());
805 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000807 if (const PointerType *PT = T->getAs<PointerType>())
808 EleTy = PT->getPointeeType();
809 else
John McCall183700f2009-09-21 23:43:11 +0000810 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek3bccf082009-07-11 00:58:27 +0000812 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
813 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000814 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000815 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000816 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000817 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000818 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000819 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000820 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
821 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000822 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000823 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000824
Ted Kremenek3bccf082009-07-11 00:58:27 +0000825 case MemRegion::ElementRegionKind: {
826 ER = cast<ElementRegion>(MR);
827 break;
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek3bccf082009-07-11 00:58:27 +0000830 // Not yet handled.
831 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000832 case MemRegion::StringRegionKind: {
833
834 }
835 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 case MemRegion::CompoundLiteralRegionKind:
837 case MemRegion::FieldRegionKind:
838 case MemRegion::ObjCObjectRegionKind:
839 case MemRegion::ObjCIvarRegionKind:
840 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek3bccf082009-07-11 00:58:27 +0000842 case MemRegion::CodeTextRegionKind:
843 // Technically this can happen if people do funny things with casts.
844 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Ted Kremenek3bccf082009-07-11 00:58:27 +0000846 case MemRegion::MemSpaceRegionKind:
847 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
848 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremenek3bccf082009-07-11 00:58:27 +0000850 case MemRegion::BEG_DECL_REGIONS:
851 case MemRegion::END_DECL_REGIONS:
852 case MemRegion::BEG_TYPED_REGIONS:
853 case MemRegion::END_TYPED_REGIONS:
854 assert(0 && "Infeasible region");
855 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000856 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000857
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000858 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000859 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000860
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000861 // For now, only support:
862 // (a) concrete integer indices that can easily be resolved
863 // (b) 0 + symbolic index
864 if (Base) {
865 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
866 // FIXME: Should use SValuator here.
867 SVal NewIdx =
868 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000869 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000870 const MemRegion* NewER =
871 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
872 ER->getSuperRegion(), getContext());
873 return ValMgr.makeLoc(NewER);
874 }
875 if (0 == Base->getValue()) {
876 const MemRegion* NewER =
877 MRMgr.getElementRegion(ER->getElementType(), R,
878 ER->getSuperRegion(), getContext());
879 return ValMgr.makeLoc(NewER);
880 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenek5dc27462009-03-03 02:51:43 +0000883 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000884}
885
Ted Kremenek9af46f52009-06-16 22:36:44 +0000886//===----------------------------------------------------------------------===//
887// Loading values from regions.
888//===----------------------------------------------------------------------===//
889
Zhongxing Xu13d50172009-10-11 08:08:02 +0000890Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
891 const MemRegion *R) {
892 if (const BindingVal *BV = B.lookup(R))
893 return Optional<SVal>::create(BV->getDirectValue());
894
895 return Optional<SVal>();
896}
897
898Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000899 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000901 if (R->isBoundable())
902 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
903 if (TR->getValueType(getContext())->isUnionType())
904 return UnknownVal();
905
Zhongxing Xu13d50172009-10-11 08:08:02 +0000906 if (BindingVal const *V = B.lookup(R))
907 return Optional<SVal>::create(V->getDefaultValue());
908
909 return Optional<SVal>();
910}
911
912Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
913 const MemRegion *R) {
914 if (const BindingVal *BV = B.lookup(R))
915 return Optional<SVal>::create(BV->getValue());
916
917 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000918}
919
Ted Kremeneka6275a52009-07-15 02:31:43 +0000920static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
921 RTy = Ctx.getCanonicalType(RTy);
922 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Ted Kremeneka6275a52009-07-15 02:31:43 +0000924 if (RTy == UsedTy)
925 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
927
Ted Kremenek25c54572009-07-20 22:58:02 +0000928 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000929 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000930 // represents a reinterpretation.
931 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000933 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000934
935 return PUsedTy && PRTy &&
936 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000937 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000938 }
939
940 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000941}
942
Ted Kremenek0954cde2009-09-24 04:11:44 +0000943const ElementRegion *
944RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
945 ASTContext &Ctx = getContext();
946 SVal idx = ValMgr.makeZeroArrayIndex();
947 assert(!T.isNull());
948 return MRMgr.getElementRegion(T, idx, SR, Ctx);
949}
950
951
952
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000953SValuator::CastResult
954RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000955
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000956 assert(!isa<UnknownVal>(L) && "location unknown");
957 assert(!isa<UndefinedVal>(L) && "location undefined");
958
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000959 // FIXME: Is this even possible? Shouldn't this be treated as a null
960 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000961 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000962 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000963
Ted Kremenek67f28532009-06-17 22:02:04 +0000964 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000965
Zhongxing Xu91844122009-05-20 09:18:48 +0000966 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000967 // Example:
968 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000969 // char* p = alloca();
970 // read(p);
971 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000972 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000973 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Ted Kremenek0954cde2009-09-24 04:11:44 +0000975 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
976 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Ted Kremenek968f0a62009-08-03 21:41:46 +0000978 if (isa<CodeTextRegion>(MR))
979 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000981 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
982 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000983 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000984 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000985
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000986 // FIXME: We should eventually handle funny addressing. e.g.:
987 //
988 // int x = ...;
989 // int *p = &x;
990 // char *q = (char*) p;
991 // char c = *q; // returns the first byte of 'x'.
992 //
993 // Such funny addressing will occur due to layering of regions.
994
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000995#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000996 ASTContext &Ctx = getContext();
997 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000998 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
999 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001000 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001001 assert(Ctx.getCanonicalType(RTy) ==
1002 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001003 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001004#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001005
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001006 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001007 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001009 // FIXME: Handle unions.
1010 if (RTy->isUnionType())
1011 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001012
1013 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001014 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001015
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001016 // FIXME: handle Vector types.
1017 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001018 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001019
1020 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001021 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001022
1023 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001024 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Ted Kremenek25c54572009-07-20 22:58:02 +00001026 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001027 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek9031dd72009-07-21 00:12:07 +00001029 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001030 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001031
Ted Kremenek451ac092009-08-06 04:50:20 +00001032 RegionBindings B = GetRegionBindings(state->getStore());
1033 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001034
1035 // Check if the region has a binding.
1036 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001037 if (SVal const *SV = V->getValue())
1038 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001039
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001040 // The location does not have a bound value. This means that it has
1041 // the value it had upon its creation and/or entry to the analyzed
1042 // function/method. These are either symbolic values or 'undefined'.
1043
Ted Kremenek356e9d62009-07-22 04:35:42 +00001044#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001045 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001046#else
1047 if (R->hasStackStorage()) {
1048#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001049 // All stack variables are considered to have undefined values
1050 // upon creation. All heap allocated blocks are considered to
1051 // have undefined values as well unless they are explicitly bound
1052 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001053 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001054 }
1055
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001056 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001057 return SValuator::CastResult(state,
1058 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001059}
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001061std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001062RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001063 if (Optional<SVal> OV = getDirectBinding(B, R))
1064 if (const nonloc::LazyCompoundVal *V =
1065 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1066 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001068 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1069 const std::pair<const GRState *, const MemRegion *> &X =
1070 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001072 if (X.first)
1073 return std::make_pair(X.first,
1074 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001075 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001076 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1077 const std::pair<const GRState *, const MemRegion *> &X =
1078 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001080 if (X.first)
1081 return std::make_pair(X.first,
1082 MRMgr.getFieldRegionWithSuper(FR, X.second));
1083 }
1084
1085 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1086}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001087
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001088SVal RegionStoreManager::RetrieveElement(const GRState* state,
1089 const ElementRegion* R) {
1090 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001091 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001092 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001093 return *V;
1094
Ted Kremenek921109a2009-07-01 23:19:52 +00001095 const MemRegion* superR = R->getSuperRegion();
1096
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001097 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001098 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001099 // FIXME: Handle loads from strings where the literal is treated as
1100 // an integer, e.g., *((unsigned int*)"hello")
1101 ASTContext &Ctx = getContext();
1102 QualType T = StrR->getValueType(Ctx)->getAs<ArrayType>()->getElementType();
1103 if (T != Ctx.getCanonicalType(R->getElementType()))
1104 return UnknownVal();
1105
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001106 const StringLiteral *Str = StrR->getStringLiteral();
1107 SVal Idx = R->getIndex();
1108 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1109 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001110 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001111 if (i > byteLength) {
1112 // Buffer overflow checking in GRExprEngine should handle this case,
1113 // but we shouldn't rely on it to not overflow here if that checking
1114 // is disabled.
1115 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001116 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001117 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001118 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001119 }
1120 }
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001122 // Special case: the current region represents a cast and it and the super
1123 // region both have pointer types or intptr_t types. If so, perform the
1124 // retrieve from the super region and appropriately "cast" the value.
1125 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001126 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001127 if (R->getIndex().isZeroConstant()) {
1128 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1129 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001130 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1131 QualType valTy = R->getValueType(Ctx);
1132 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1133 // Retrieve the value from the super region. This will be casted to
1134 // valTy when we return to 'Retrieve'.
1135 const SValuator::CastResult &cr = Retrieve(state,
1136 loc::MemRegionVal(superR),
1137 valTy);
1138 return cr.getSVal();
1139 }
1140 }
1141 }
1142 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001143
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001144 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001145 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001146 if (SymbolRef parentSym = V->getAsSymbol())
1147 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001148
1149 if (V->isUnknownOrUndef())
1150 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001151
1152 // Handle LazyCompoundVals for the immediate super region. Other cases
1153 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001154 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001155 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001157 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1158 return RetrieveElement(LCV->getState(), R);
1159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremeneka6275a52009-07-15 02:31:43 +00001161 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001162 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001163 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001164
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001165 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001166}
1167
Mike Stump1eb44332009-09-09 15:08:12 +00001168SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001169 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001170
1171 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001172 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001173 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001174 return *V;
1175
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001176 QualType Ty = R->getValueType(getContext());
1177 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1178}
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001180SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1181 const TypedRegion *R,
1182 QualType Ty,
1183 const MemRegion *superR) {
1184
Mike Stump1eb44332009-09-09 15:08:12 +00001185 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001186 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001188 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001190 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001191 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001192 if (SymbolRef parentSym = D->getAsSymbol())
1193 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001195 if (D->isZeroConstant())
1196 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001198 if (D->isUnknown())
1199 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001201 assert(0 && "Unknown default value");
1202 }
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001204 // If our super region is a field or element itself, walk up the region
1205 // hierarchy to see if there is a default value installed in an ancestor.
1206 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1207 superR = cast<SubRegion>(superR)->getSuperRegion();
1208 continue;
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001211 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001214 // Lazy binding?
1215 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001216 const MemRegion *lazyBindingRegion = NULL;
1217 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001219 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001220 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001222 if (isa<ElementRegion>(R))
1223 return RetrieveElement(lazyBindingState,
1224 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001226 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001227 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001228 }
1229
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001230 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001232 if (isa<ElementRegion>(R)) {
1233 // Currently we don't reason specially about Clang-style vectors. Check
1234 // if superR is a vector and if so return Unknown.
1235 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1236 if (typedSuperR->getValueType(getContext())->isVectorType())
1237 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001238 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001241 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001244 // All other values are symbolic.
1245 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001246}
Mike Stump1eb44332009-09-09 15:08:12 +00001247
1248SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001249 const ObjCIvarRegion* R) {
1250
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001251 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001252 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001253
Zhongxing Xu13d50172009-10-11 08:08:02 +00001254 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001255 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001257 const MemRegion *superR = R->getSuperRegion();
1258
1259 // Check if the super region has a binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001260 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001261 if (SymbolRef parentSym = V->getAsSymbol())
1262 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001264 // Other cases: give up.
1265 return UnknownVal();
1266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek25c54572009-07-20 22:58:02 +00001268 return RetrieveLazySymbol(state, R);
1269}
1270
Ted Kremenek9031dd72009-07-21 00:12:07 +00001271SVal RegionStoreManager::RetrieveVar(const GRState *state,
1272 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Ted Kremenek9031dd72009-07-21 00:12:07 +00001274 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001275 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Zhongxing Xu13d50172009-10-11 08:08:02 +00001277 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001278 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek9031dd72009-07-21 00:12:07 +00001280 // Lazily derive a value for the VarRegion.
1281 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283 if (R->hasGlobalsOrParametersStorage())
1284 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenek9031dd72009-07-21 00:12:07 +00001286 return UndefinedVal();
1287}
1288
Mike Stump1eb44332009-09-09 15:08:12 +00001289SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001290 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek25c54572009-07-20 22:58:02 +00001292 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001293
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001294 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001295 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001296}
1297
Mike Stump1eb44332009-09-09 15:08:12 +00001298SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1299 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001300 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001301 assert(T->isStructureType());
1302
Zhongxing Xub7507d12009-06-11 07:27:30 +00001303 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001304 RecordDecl* RD = RT->getDecl();
1305 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001306 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001307#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001308 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1309
Ted Kremenek67f28532009-06-17 22:02:04 +00001310 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1311 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001312 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001313
Douglas Gregore267ff32008-12-11 20:41:00 +00001314 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1315 FieldEnd = Fields.rend();
1316 Field != FieldEnd; ++Field) {
1317 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001318 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001319 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001320 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1321 }
1322
Zhongxing Xud91ee272009-06-23 09:02:15 +00001323 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001324#else
1325 return ValMgr.makeLazyCompoundVal(state, R);
1326#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001327}
1328
Ted Kremenek67f28532009-06-17 22:02:04 +00001329SVal RegionStoreManager::RetrieveArray(const GRState *state,
1330 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001331#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001332 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001333 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1334
1335 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001336 uint64_t size = CAT->getSize().getZExtValue();
1337 for (uint64_t i = 0; i < size; ++i) {
1338 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001339 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001340 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001341 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001342 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001343 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1344 }
1345
Zhongxing Xud91ee272009-06-23 09:02:15 +00001346 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001347#else
1348 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1349 return ValMgr.makeLazyCompoundVal(state, R);
1350#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001351}
1352
Ted Kremenek9af46f52009-06-16 22:36:44 +00001353//===----------------------------------------------------------------------===//
1354// Binding values to regions.
1355//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001356
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001357Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001358 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenek0964a062009-01-21 06:57:53 +00001360 if (isa<loc::MemRegionVal>(L))
1361 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Ted Kremenek0964a062009-01-21 06:57:53 +00001363 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001364 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001365 return RBFactory.Remove(B, R).getRoot();
1366 }
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Ted Kremenek0964a062009-01-21 06:57:53 +00001368 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001369}
1370
Ted Kremenek67f28532009-06-17 22:02:04 +00001371const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001372 if (isa<loc::ConcreteInt>(L))
1373 return state;
1374
Ted Kremenek9af46f52009-06-16 22:36:44 +00001375 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001376 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378 // Check if the region is a struct region.
1379 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1380 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001381 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001383 // Special case: the current region represents a cast and it and the super
1384 // region both have pointer types or intptr_t types. If so, perform the
1385 // bind to the super region.
1386 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001387 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001388 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1389 if (ER->getIndex().isZeroConstant()) {
1390 if (const TypedRegion *superR =
1391 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1392 ASTContext &Ctx = getContext();
1393 QualType superTy = superR->getValueType(Ctx);
1394 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
1396 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001397 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001398 SValuator::CastResult cr =
1399 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001400 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1401 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001402 // For now, just invalidate the fields of the struct/union/class.
1403 // FIXME: Precisely handle the fields of the record.
1404 if (superTy->isRecordType())
1405 return InvalidateRegion(state, superR, NULL, 0);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001406 }
1407 }
1408 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001409 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1410 // Binding directly to a symbolic region should be treated as binding
1411 // to element 0.
1412 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001413 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001414 R = GetElementZeroRegion(SR, T);
1415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001417 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001418 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001419 return state->makeWithStore(
1420 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001421}
1422
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001423const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1424 const VarDecl *VD,
1425 const LocationContext *LC,
1426 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001427
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001428 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001429 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001430
Ted Kremenek0964a062009-01-21 06:57:53 +00001431 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001432 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001433 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001434 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001435
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001436 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001437}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001438
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001439// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001440const GRState *
1441RegionStoreManager::BindCompoundLiteral(const GRState *state,
1442 const CompoundLiteralExpr* CL,
1443 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001445 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001446 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001447}
1448
Ted Kremenek67f28532009-06-17 22:02:04 +00001449const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001450 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001451 SVal Init) {
1452
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001453 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001454 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001455 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001456
Ted Kremenek46537392009-07-16 01:33:37 +00001457 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001458
1459 // Check if the init expr is a StringLiteral.
1460 if (isa<loc::MemRegionVal>(Init)) {
1461 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1462 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1463 const char* str = S->getStrData();
1464 unsigned len = S->getByteLength();
1465 unsigned j = 0;
1466
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001467 // Copy bytes from the string literal into the target array. Trailing bytes
1468 // in the array that are not covered by the string literal are initialized
1469 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001470 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001471 if (j >= len)
1472 break;
1473
Ted Kremenek46537392009-07-16 01:33:37 +00001474 SVal Idx = ValMgr.makeArrayIndex(i);
1475 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1476 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001477
Zhongxing Xud91ee272009-06-23 09:02:15 +00001478 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001479 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001480 }
1481
Ted Kremenek67f28532009-06-17 22:02:04 +00001482 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001483 }
1484
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001485 // Handle lazy compound values.
1486 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1487 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001488
1489 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001490 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001491 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001492 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek46537392009-07-16 01:33:37 +00001494 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001495 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001496 if (VI == VE)
1497 break;
1498
Ted Kremenek46537392009-07-16 01:33:37 +00001499 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001500 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001501
1502 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001503 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001504 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001505 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001506 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001507 }
1508
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001509 // If the init list is shorter than the array length, set the array default
1510 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001511 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001512 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001513 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001514 Store store = state->getStore();
1515 RegionBindings B = GetRegionBindings(store);
1516 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1517 state = state->makeWithStore(B.getRoot());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001518 }
1519 }
1520
Ted Kremenek67f28532009-06-17 22:02:04 +00001521 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001522}
1523
Ted Kremenek67f28532009-06-17 22:02:04 +00001524const GRState *
1525RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1526 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Ted Kremenek67f28532009-06-17 22:02:04 +00001528 if (!Features.supportsFields())
1529 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001531 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001532 assert(T->isStructureType());
1533
Ted Kremenek6217b802009-07-29 21:53:49 +00001534 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001535 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001536
1537 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001538 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001539
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001540 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001541 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001542 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Ted Kremenek67f28532009-06-17 22:02:04 +00001544 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1545 // Ignore them and kill the field values.
1546 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001547 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001548
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001549 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001550 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001551
1552 RecordDecl::field_iterator FI, FE;
1553
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001554 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001555
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001556 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001557 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001558
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001559 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001560 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001561
Ted Kremenekcf549592009-09-22 21:19:14 +00001562 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001563 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001564 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001565 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001566 else
1567 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001568 }
1569
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001570 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001571 if (FI != FE) {
1572 Store store = state->getStore();
1573 RegionBindings B = GetRegionBindings(store);
1574 B = RBFactory.Add(B, R,
1575 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1576 state = state->makeWithStore(B.getRoot());
1577 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001578
Ted Kremenek67f28532009-06-17 22:02:04 +00001579 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001580}
1581
Zhongxing Xu13d50172009-10-11 08:08:02 +00001582Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1583 RegionBindings B = GetRegionBindings(store);
1584 llvm::OwningPtr<RegionStoreSubRegionMap>
1585 SubRegions(getRegionStoreSubRegionMap(store));
1586 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001587
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001588 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001589 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001590
Zhongxing Xu13d50172009-10-11 08:08:02 +00001591 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001592}
1593
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001594const GRState*
1595RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1596 const GRState *state,
1597 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001598
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001599 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001600 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001601
Mike Stump1eb44332009-09-09 15:08:12 +00001602 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001603 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001604
Mike Stump1eb44332009-09-09 15:08:12 +00001605 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001606 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001608 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1609 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001610 return state->makeWithStore(
1611 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001612}
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Ted Kremenek9af46f52009-06-16 22:36:44 +00001614//===----------------------------------------------------------------------===//
1615// State pruning.
1616//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Ted Kremenek451ac092009-08-06 04:50:20 +00001618namespace {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001619class VISIBILITY_HIDDEN RBDNode
1620 : public std::pair<const GRState*, const MemRegion *> {
Ted Kremenek451ac092009-08-06 04:50:20 +00001621public:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001622 RBDNode(const GRState *st, const MemRegion *r)
1623 : std::pair<const GRState*, const MemRegion*>(st, r) {}
1624
1625 const GRState *getState() const { return first; }
1626 const MemRegion *getRegion() const { return second; }
1627};
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001629enum VisitFlag { NotVisited = 0, VisitedFromSubRegion, VisitedFromSuperRegion };
1630
1631class RBDItem : public RBDNode {
1632private:
1633 const VisitFlag VF;
1634
1635public:
1636 RBDItem(const GRState *st, const MemRegion *r, VisitFlag vf)
1637 : RBDNode(st, r), VF(vf) {}
1638
1639 VisitFlag getVisitFlag() const { return VF; }
Ted Kremenek451ac092009-08-06 04:50:20 +00001640};
1641} // end anonymous namespace
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001642
Mike Stump1eb44332009-09-09 15:08:12 +00001643void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001644 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001645 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001646{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001647 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001648 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Ted Kremenek9af46f52009-06-16 22:36:44 +00001650 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001651 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001652 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001653
1654 // Do a pass over the regions in the store. For VarRegions we check if
1655 // the variable is still live and if so add it to the list of live roots.
1656 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001657 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001658
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001659 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001660 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001661 const MemRegion *R = I.getKey();
1662 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001663 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001664
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001665 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001666 // real roots.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001667 llvm::SmallVector<RBDItem, 10> WorkList;
1668 llvm::DenseMap<const MemRegion*,unsigned> IntermediateVisited;
1669
Ted Kremenek9af46f52009-06-16 22:36:44 +00001670 while (!IntermediateRoots.empty()) {
1671 const MemRegion* R = IntermediateRoots.back();
1672 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001673
1674 unsigned &visited = IntermediateVisited[R];
1675 if (visited)
1676 continue;
1677 visited = 1;
1678
Ted Kremenek9af46f52009-06-16 22:36:44 +00001679 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001680 if (SymReaper.isLive(Loc, VR->getDecl()))
1681 WorkList.push_back(RBDItem(&state, VR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 continue;
1683 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001684
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001685 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001686 if (SymReaper.isLive(SR->getSymbol()))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001687 WorkList.push_back(RBDItem(&state, SR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001688 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001689 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001690
1691 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001692 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001693 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001694 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001697 // Enqueue the RegionRoots onto WorkList.
1698 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1699 E=RegionRoots.end(); I!=E; ++I) {
1700 WorkList.push_back(RBDItem(&state, *I, VisitedFromSuperRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001701 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001702 RegionRoots.clear();
1703
1704 // Process the worklist.
1705 typedef llvm::DenseMap<std::pair<const GRState*, const MemRegion*>, VisitFlag>
1706 VisitMap;
1707
1708 VisitMap Visited;
1709
1710 while (!WorkList.empty()) {
1711 RBDItem N = WorkList.back();
1712 WorkList.pop_back();
1713
1714 // Have we visited this node before?
1715 VisitFlag &VF = Visited[N];
1716 if (VF >= N.getVisitFlag())
1717 continue;
1718
1719 const MemRegion *R = N.getRegion();
1720 const GRState *state_N = N.getState();
1721
1722 // Enqueue subregions?
1723 if (N.getVisitFlag() == VisitedFromSuperRegion) {
1724 RegionStoreSubRegionMap *M;
1725
1726 if (&state == state_N)
1727 M = SubRegions.get();
1728 else {
1729 RegionStoreSubRegionMap *& SM = SC[state_N];
1730 if (!SM)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001731 SM = getRegionStoreSubRegionMap(state_N->getStore());
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001732 M = SM;
1733 }
1734
1735 RegionStoreSubRegionMap::iterator I, E;
1736 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1737 WorkList.push_back(RBDItem(state_N, *I, VisitedFromSuperRegion));
1738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001740 // At this point, if we have already visited this region before, we are
1741 // done.
1742 if (VF != NotVisited) {
1743 VF = N.getVisitFlag();
1744 continue;
1745 }
1746 VF = N.getVisitFlag();
1747
1748 // Enqueue the super region.
1749 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1750 const MemRegion *superR = SR->getSuperRegion();
1751 if (!isa<MemSpaceRegion>(superR)) {
1752 // If 'R' is a field or an element, we want to keep the bindings
1753 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001754 // pointer arithmetic can get us to the other fields or elements.
1755 // FIXME: add an assertion that this is always true.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001756 VisitFlag NewVisit =
1757 isa<FieldRegion>(R) || isa<ElementRegion>(R) || isa<ObjCIvarRegion>(R)
1758 ? VisitedFromSuperRegion : VisitedFromSubRegion;
1759
1760 WorkList.push_back(RBDItem(state_N, superR, NewVisit));
1761 }
1762 }
1763
1764 // Mark the symbol for any live SymbolicRegion as "live". This means we
1765 // should continue to track that symbol.
1766 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1767 SymReaper.markLive(SymR->getSymbol());
1768
1769 Store store_N = state_N->getStore();
1770 RegionBindings B_N = GetRegionBindings(store_N);
1771
1772 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001773 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001774
Zhongxing Xu13d50172009-10-11 08:08:02 +00001775 if (V) {
1776 // Check for lazy bindings.
1777 if (const nonloc::LazyCompoundVal *LCV =
1778 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001779
Zhongxing Xu13d50172009-10-11 08:08:02 +00001780 const LazyCompoundValData *D = LCV->getCVData();
1781 WorkList.push_back(RBDItem(D->getState(), D->getRegion(),
1782 VisitedFromSuperRegion));
1783 }
1784 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001785 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001786 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001787 SI!=SE;++SI)
1788 SymReaper.markLive(*SI);
1789
Zhongxing Xu13d50172009-10-11 08:08:02 +00001790 // If V is a region, then add it to the worklist.
1791 if (const MemRegion *RX = V->getAsRegion())
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001792 WorkList.push_back(RBDItem(state_N, RX, VisitedFromSuperRegion));
1793 }
1794 }
1795 }
1796
Ted Kremenek9af46f52009-06-16 22:36:44 +00001797 // We have now scanned the store, marking reachable regions and symbols
1798 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001799 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001800 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001801 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001802 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001803 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001807 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Ted Kremenek9af46f52009-06-16 22:36:44 +00001809 // Mark all non-live symbols that this region references as dead.
1810 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1811 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Zhongxing Xu13d50172009-10-11 08:08:02 +00001813 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001814 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1815 for (; SI != SE; ++SI)
1816 SymReaper.maybeDead(*SI);
1817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001819 // Write the store back.
1820 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001821}
1822
1823//===----------------------------------------------------------------------===//
1824// Utility methods.
1825//===----------------------------------------------------------------------===//
1826
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001827void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001828 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001829 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001830 OS << "Store (direct bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek451ac092009-08-06 04:50:20 +00001832 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001833 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001834}