blob: fb8ee354b9b8e0a4729807db1acc7218960ddb7e [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 Xubaf03a72008-11-24 09:44:56 +000035// Actual Store type.
Ted Kremenek451ac092009-08-06 04:50:20 +000036typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000037
Ted Kremenek50dc1b32008-12-24 01:05:03 +000038//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000039// Fine-grained control of RegionStoreManager.
40//===----------------------------------------------------------------------===//
41
42namespace {
43struct VISIBILITY_HIDDEN minimal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000044struct VISIBILITY_HIDDEN maximal_features_tag {};
45
Ted Kremenek9af46f52009-06-16 22:36:44 +000046class VISIBILITY_HIDDEN RegionStoreFeatures {
47 bool SupportsFields;
48 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenek9af46f52009-06-16 22:36:44 +000050public:
51 RegionStoreFeatures(minimal_features_tag) :
52 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek9af46f52009-06-16 22:36:44 +000054 RegionStoreFeatures(maximal_features_tag) :
55 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000056
Ted Kremenek9af46f52009-06-16 22:36:44 +000057 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenek9af46f52009-06-16 22:36:44 +000059 bool supportsFields() const { return SupportsFields; }
60 bool supportsRemaining() const { return SupportsRemaining; }
61};
62}
63
64//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000065// Region "Extents"
66//===----------------------------------------------------------------------===//
67//
68// MemRegions represent chunks of memory with a size (their "extent"). This
69// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000070//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000071namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
72static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000073namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000074 template<> struct GRStateTrait<RegionExtents>
75 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
76 static void* GDMIndex() { return &RegionExtentsIndex; }
77 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000078}
79
Ted Kremenek50dc1b32008-12-24 01:05:03 +000080//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000081// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000082//===----------------------------------------------------------------------===//
83//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000084// This GDM entry tracks what regions have a default value if they have no bound
85// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000087namespace {
88class VISIBILITY_HIDDEN RegionDefaultValue {
89public:
90 typedef llvm::ImmutableMap<const MemRegion*, SVal> MapTy;
91};
92}
Ted Kremenek50dc1b32008-12-24 01:05:03 +000093static int RegionDefaultValueIndex = 0;
94namespace clang {
95 template<> struct GRStateTrait<RegionDefaultValue>
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000096 : public GRStatePartialTrait<RegionDefaultValue::MapTy> {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000097 static void* GDMIndex() { return &RegionDefaultValueIndex; }
98 };
99}
100
Ted Kremenek451ac092009-08-06 04:50:20 +0000101typedef RegionDefaultValue::MapTy RegionDefaultBindings;
102
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000103//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000104// Utility functions.
105//===----------------------------------------------------------------------===//
106
107static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
108 if (ty->isAnyPointerType())
109 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000111 return ty->isIntegerType() && ty->isScalarType() &&
112 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
113}
114
115//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000116// Main RegionStore logic.
117//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000118
Zhongxing Xu17892752008-10-08 02:50:44 +0000119namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
122 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000123 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000124 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000125 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000126public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000127 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000128 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000129
130 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000131 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000132 return true;
133 }
134
135 I->second = F.Add(I->second, SubRegion);
136 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000137 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000139 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek59e8f112009-03-03 01:35:36 +0000141 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Ted Kremenek5dc27462009-03-03 02:51:43 +0000143 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 Map::iterator I = M.find(Parent);
145
146 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000147 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149 llvm::ImmutableSet<const MemRegion*> S = I->second;
150 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
151 SI != SE; ++SI) {
152 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000153 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek5dc27462009-03-03 02:51:43 +0000156 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000159 typedef SetTy::iterator iterator;
160
161 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
162 Map::iterator I = M.find(R);
163 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
164 return std::make_pair(S.begin(), S.end());
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167
Zhongxing Xu17892752008-10-08 02:50:44 +0000168class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000169 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000170 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000171
172 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
173 SMCache SC;
174
Zhongxing Xu17892752008-10-08 02:50:44 +0000175public:
Mike Stump1eb44332009-09-09 15:08:12 +0000176 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000177 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000178 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000179 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000180
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000181 virtual ~RegionStoreManager() {
182 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
183 delete (*I).second;
184 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000185
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000186 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000188 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
190
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000191 /// getDefaultBinding - Returns an SVal* representing an optional default
192 /// binding associated with a region and its subregions.
193 Optional<SVal> getDefaultBinding(const GRState *state, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000195 /// getLValueString - Returns an SVal representing the lvalue of a
196 /// StringLiteral. Within RegionStore a StringLiteral has an
197 /// associated StringRegion, and the lvalue of a StringLiteral is
198 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000199 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000200
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000201 /// getLValueCompoundLiteral - Returns an SVal representing the
202 /// lvalue of a compound literal. Within RegionStore a compound
203 /// literal has an associated region, and the lvalue of the
204 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000205 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000206
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000207 /// getLValueVar - Returns an SVal that represents the lvalue of a
208 /// variable. Within RegionStore a variable has an associated
209 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000210 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
211 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek67f28532009-06-17 22:02:04 +0000213 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000214
Ted Kremenek67f28532009-06-17 22:02:04 +0000215 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek67f28532009-06-17 22:02:04 +0000217 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000218
Ted Kremenek67f28532009-06-17 22:02:04 +0000219 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000220 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000221
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000222
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000223 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
224 /// type. 'Array' represents the lvalue of the array being decayed
225 /// to a pointer, and the returned SVal represents the decayed
226 /// version of that lvalue (i.e., a pointer to the first element of
227 /// the array). This is called by GRExprEngine when evaluating
228 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000229 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000230
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000231 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000232 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000233
Mike Stump1eb44332009-09-09 15:08:12 +0000234 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000235 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000236 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000237
Ted Kremenek67f28532009-06-17 22:02:04 +0000238 //===-------------------------------------------------------------------===//
239 // Binding values to regions.
240 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000241
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000242 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
243 const Expr *E, unsigned Count);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000245private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000246 void RemoveSubRegionBindings(RegionBindings &B,
247 RegionDefaultBindings &DVM,
248 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000249 const MemRegion *R,
250 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
252public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000253 const GRState *Bind(const GRState *state, Loc LV, SVal V);
254
255 const GRState *BindCompoundLiteral(const GRState *state,
256 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000258 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
259 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000260
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000261 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
262 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000263 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000264 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 /// BindStruct - Bind a compound value to a structure.
267 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek67f28532009-06-17 22:02:04 +0000269 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
271 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek67f28532009-06-17 22:02:04 +0000272 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
273
Ted Kremenek67f28532009-06-17 22:02:04 +0000274 Store Remove(Store store, Loc LV);
275
276 //===------------------------------------------------------------------===//
277 // Loading values from regions.
278 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Ted Kremenek67f28532009-06-17 22:02:04 +0000280 /// The high level logic for this method is this:
281 /// Retrieve (L)
282 /// if L has binding
283 /// return L's binding
284 /// else if L is in killset
285 /// return unknown
286 /// else
287 /// if L is on stack or heap
288 /// return undefined
289 /// else
290 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000291 SValuator::CastResult Retrieve(const GRState *state, Loc L,
292 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000293
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000294 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000295
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000296 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000298 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek9031dd72009-07-21 00:12:07 +0000300 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenek25c54572009-07-20 22:58:02 +0000302 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000304 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
305 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenek67f28532009-06-17 22:02:04 +0000307 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000308 /// struct copy:
309 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000310 /// x = y;
311 /// y's value is retrieved by this method.
312 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek67f28532009-06-17 22:02:04 +0000314 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000316 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000317 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000319 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
320 const GRState *state,
321 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000322
Ted Kremenek0954cde2009-09-24 04:11:44 +0000323 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
324 QualType T);
325
Ted Kremenek67f28532009-06-17 22:02:04 +0000326 //===------------------------------------------------------------------===//
327 // State pruning.
328 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek67f28532009-06-17 22:02:04 +0000330 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
331 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000332 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000333 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
334
335 //===------------------------------------------------------------------===//
336 // Region "extents".
337 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek67f28532009-06-17 22:02:04 +0000339 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
340 SVal getSizeInElements(const GRState *state, const MemRegion* R);
341
342 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000343 // Utility methods.
344 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremenek451ac092009-08-06 04:50:20 +0000346 static inline RegionBindings GetRegionBindings(Store store) {
347 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000348 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000349
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000350 void print(Store store, llvm::raw_ostream& Out, const char* nl,
351 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000352
353 void iterBindings(Store store, BindingsHandler& f) {
354 // FIXME: Implement.
355 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 // FIXME: Remove.
358 BasicValueFactory& getBasicVals() {
359 return StateMgr.getBasicVals();
360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Ted Kremenek67f28532009-06-17 22:02:04 +0000362 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000363 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000364};
365
366} // end anonymous namespace
367
Ted Kremenek9af46f52009-06-16 22:36:44 +0000368//===----------------------------------------------------------------------===//
369// RegionStore creation.
370//===----------------------------------------------------------------------===//
371
372StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
373 RegionStoreFeatures F = maximal_features_tag();
374 return new RegionStoreManager(StMgr, F);
375}
376
377StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
378 RegionStoreFeatures F = minimal_features_tag();
379 F.enableFields(true);
380 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000381}
382
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000383void
384RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000385 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000386 const MemRegion *superR = R->getSuperRegion();
387 if (add(superR, R))
388 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000389 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000390}
391
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000392RegionStoreSubRegionMap*
393RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000394 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000395 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000397 llvm::SmallVector<const SubRegion*, 10> WL;
398
Ted Kremenek451ac092009-08-06 04:50:20 +0000399 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000400 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
401 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Ted Kremenek451ac092009-08-06 04:50:20 +0000403 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
404 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000405 I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000406 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
407 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000408
Mike Stump1eb44332009-09-09 15:08:12 +0000409 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000410 // don't have direct bindings but are super regions of those that do.
411 while (!WL.empty()) {
412 const SubRegion *R = WL.back();
413 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000415 }
416
Ted Kremenek14453bf2009-03-03 19:02:42 +0000417 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000418}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000419
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000420SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
421 return getRegionStoreSubRegionMap(state);
422}
423
Ted Kremenek9af46f52009-06-16 22:36:44 +0000424//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000425// Binding invalidation.
426//===----------------------------------------------------------------------===//
427
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428void
Ted Kremenek451ac092009-08-06 04:50:20 +0000429RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
430 RegionDefaultBindings &DVM,
431 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000432 const MemRegion *R,
433 RegionStoreSubRegionMap &M) {
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000435 RegionStoreSubRegionMap::iterator I, E;
436
437 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000438 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000440 B = RBFactory.Remove(B, R);
441 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000442}
443
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000444const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
445 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000446 const Expr *Ex,
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000447 unsigned Count) {
448 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000450 // Strip away casts.
451 R = R->getBaseRegion();
452
Ted Kremenek87806792009-09-27 20:45:21 +0000453 // Get the mapping of regions -> subregions.
454 llvm::OwningPtr<RegionStoreSubRegionMap>
455 SubRegions(getRegionStoreSubRegionMap(state));
456
457 RegionBindings B = GetRegionBindings(state->getStore());
458 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
459 RegionDefaultBindings::Factory &DVMFactory =
460 state->get_context<RegionDefaultValue>();
461
462 llvm::DenseMap<const MemRegion *, unsigned> Visited;
463 llvm::SmallVector<const MemRegion *, 10> WorkList;
464 WorkList.push_back(R);
465
466 while (!WorkList.empty()) {
467 R = WorkList.back();
468 WorkList.pop_back();
469
470 // Have we visited this region before?
471 unsigned &visited = Visited[R];
472 if (visited)
473 continue;
474 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Ted Kremenek87806792009-09-27 20:45:21 +0000476 // Add subregions to work list.
477 RegionStoreSubRegionMap::iterator I, E;
478 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
479 WorkList.push_back(*I);
480
481 // Handle region.
482 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
483 isa<ObjCObjectRegion>(R)) {
484 // Invalidate the region by setting its default value to
485 // conjured symbol. The type of the symbol is irrelavant.
486 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
487 Count);
488 DVM = DVMFactory.Add(DVM, R, V);
489 continue;
490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Ted Kremenek87806792009-09-27 20:45:21 +0000492 if (!R->isBoundable())
493 continue;
494
495 const TypedRegion *TR = cast<TypedRegion>(R);
496 QualType T = TR->getValueType(Ctx);
497
498 if (const RecordType *RT = T->getAsStructureType()) {
499 // FIXME: handle structs with default region value.
500 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
501
502 // No record definition. There is nothing we can do.
503 if (!RD)
504 continue;
505
506 // Invalidate the region by setting its default value to
507 // conjured symbol. The type of the symbol is irrelavant.
508 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
509 Count);
510 DVM = DVMFactory.Add(DVM, R, V);
511 continue;
512 }
513
514 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
515 // Set the default value of the array to conjured symbol.
516 DefinedOrUnknownSVal V =
517 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
518 DVM = DVMFactory.Add(DVM, R, V);
519 continue;
520 }
521
522 // Get the old binding. Is it a region? If so, add it to the worklist.
523 if (const SVal *OldV = B.lookup(R)) {
524 if (const MemRegion *RV = OldV->getAsRegion())
525 WorkList.push_back(RV);
526 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000527
528 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
529 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
530 // For fields and elements whose super region has also been invalidated,
531 // only remove the old binding. The super region will get set with a
532 // default value from which we can lazily derive a new symbolic value.
533 B = RBFactory.Remove(B, R);
534 continue;
535 }
Ted Kremenek87806792009-09-27 20:45:21 +0000536
Ted Kremenek389c44c2009-09-29 03:12:50 +0000537 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000538 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
539 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
540 B = RBFactory.Add(B, R, V);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000541 }
542
Ted Kremenek87806792009-09-27 20:45:21 +0000543 // Create a new state with the updated bindings.
544 return state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000545}
546
547//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000548// getLValueXXX methods.
549//===----------------------------------------------------------------------===//
550
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000551/// getLValueString - Returns an SVal representing the lvalue of a
552/// StringLiteral. Within RegionStore a StringLiteral has an
553/// associated StringRegion, and the lvalue of a StringLiteral is the
554/// lvalue of that region.
Mike Stump1eb44332009-09-09 15:08:12 +0000555SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000556 const StringLiteral* S) {
557 return loc::MemRegionVal(MRMgr.getStringRegion(S));
558}
559
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000560/// getLValueVar - Returns an SVal that represents the lvalue of a
561/// variable. Within RegionStore a variable has an associated
562/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000563SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
564 const LocationContext *LC) {
565 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000566}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000567
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000568/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
569/// of a compound literal. Within RegionStore a compound literal
570/// has an associated region, and the lvalue of the compound literal
571/// is the lvalue of that region.
572SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000573RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Mike Stump1eb44332009-09-09 15:08:12 +0000574 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000575 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
576}
577
Ted Kremenek67f28532009-06-17 22:02:04 +0000578SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000579 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000580 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000581}
582
Ted Kremenek67f28532009-06-17 22:02:04 +0000583SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000584 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000585 return getLValueFieldOrIvar(St, Base, D);
586}
587
Ted Kremenek67f28532009-06-17 22:02:04 +0000588SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000589 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000590 if (Base.isUnknownOrUndef())
591 return Base;
592
593 Loc BaseL = cast<Loc>(Base);
594 const MemRegion* BaseR = 0;
595
596 switch (BaseL.getSubKind()) {
597 case loc::MemRegionKind:
598 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
599 break;
600
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000601 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000602 // These are anormal cases. Flag an undefined value.
603 return UndefinedVal();
604
605 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000606 // While these seem funny, this can happen through casts.
607 // FIXME: What we should return is the field offset. For example,
608 // add the field offset to the integer value. That way funny things
609 // like this work properly: &(((struct foo *) 0xa)->f)
610 return Base;
611
612 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000613 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000614 return Base;
615 }
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000617 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
618 // of FieldDecl.
619 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
620 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000621
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000622 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000623}
624
Ted Kremenek67f28532009-06-17 22:02:04 +0000625SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000626 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000627 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000628
Ted Kremenekde7ec632009-03-09 22:44:49 +0000629 // If the base is an unknown or undefined value, just return it back.
630 // FIXME: For absolute pointer addresses, we just return that value back as
631 // well, although in reality we should return the offset added to that
632 // value.
633 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000634 return Base;
635
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000636 // Only handle integer offsets... for now.
637 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000638 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000639
Zhongxing Xuce760782009-05-09 13:20:07 +0000640 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000641
642 // Pointer of any type can be cast and used as array base.
643 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Ted Kremenek46537392009-07-16 01:33:37 +0000645 // Convert the offset to the appropriate size and signedness.
646 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000647
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000648 if (!ElemR) {
649 //
650 // If the base region is not an ElementRegion, create one.
651 // This can happen in the following example:
652 //
653 // char *p = __builtin_alloc(10);
654 // p[1] = 8;
655 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000656 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000657 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000658 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000659 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000660 }
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000662 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000664 if (!isa<nonloc::ConcreteInt>(BaseIdx))
665 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000667 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
668 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
669 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremenek46537392009-07-16 01:33:37 +0000671 // Compute the new index.
672 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Ted Kremenek46537392009-07-16 01:33:37 +0000674 // Construct the new ElementRegion.
675 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000676 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000677 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000678}
679
Ted Kremenek9af46f52009-06-16 22:36:44 +0000680//===----------------------------------------------------------------------===//
681// Extents for regions.
682//===----------------------------------------------------------------------===//
683
Ted Kremenek67f28532009-06-17 22:02:04 +0000684SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000685 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000687 switch (R->getKind()) {
688 case MemRegion::MemSpaceRegionKind:
689 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000690 return UnknownVal();
691
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000692 case MemRegion::CodeTextRegionKind:
693 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000694 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000695
696 // Not yet handled.
697 case MemRegion::AllocaRegionKind:
698 case MemRegion::CompoundLiteralRegionKind:
699 case MemRegion::ElementRegionKind:
700 case MemRegion::FieldRegionKind:
701 case MemRegion::ObjCIvarRegionKind:
702 case MemRegion::ObjCObjectRegionKind:
703 case MemRegion::SymbolicRegionKind:
704 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000706 case MemRegion::StringRegionKind: {
707 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000708 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000709 // operations with signed indices.
710 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000713 case MemRegion::VarRegionKind: {
714 const VarRegion* VR = cast<VarRegion>(R);
715 // Get the type of the variable.
716 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000718 // FIXME: Handle variable-length arrays.
719 if (isa<VariableArrayType>(T))
720 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000722 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
723 // return the size as signed integer.
724 return ValMgr.makeIntVal(CAT->getSize(), false);
725 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000726
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000727 // Clients can use ordinary variables as if they were arrays. These
728 // essentially are arrays of size 1.
729 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000732 case MemRegion::BEG_DECL_REGIONS:
733 case MemRegion::END_DECL_REGIONS:
734 case MemRegion::BEG_TYPED_REGIONS:
735 case MemRegion::END_TYPED_REGIONS:
736 assert(0 && "Infeasible region");
737 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000740 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000741 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000742}
743
Ted Kremenek67f28532009-06-17 22:02:04 +0000744const GRState *RegionStoreManager::setExtent(const GRState *state,
745 const MemRegion *region,
746 SVal extent) {
747 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000748}
749
750//===----------------------------------------------------------------------===//
751// Location and region casting.
752//===----------------------------------------------------------------------===//
753
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000754/// ArrayToPointer - Emulates the "decay" of an array to a pointer
755/// type. 'Array' represents the lvalue of the array being decayed
756/// to a pointer, and the returned SVal represents the decayed
757/// version of that lvalue (i.e., a pointer to the first element of
758/// the array). This is called by GRExprEngine when evaluating casts
759/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000760SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000761 if (!isa<loc::MemRegionVal>(Array))
762 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenekabb042f2008-12-13 19:24:37 +0000764 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
765 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000767 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000768 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000770 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000771 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000772 ArrayType *AT = cast<ArrayType>(T);
773 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenek75185b52009-07-16 00:00:11 +0000775 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
776 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000777
778 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000779}
780
Ted Kremenek9af46f52009-06-16 22:36:44 +0000781//===----------------------------------------------------------------------===//
782// Pointer arithmetic.
783//===----------------------------------------------------------------------===//
784
Mike Stump1eb44332009-09-09 15:08:12 +0000785SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000786 BinaryOperator::Opcode Op, Loc L, NonLoc R,
787 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000788 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000789 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000790 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000791
Zhongxing Xua1718c72009-04-03 07:33:13 +0000792 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000793 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000794
Ted Kremenek3bccf082009-07-11 00:58:27 +0000795 switch (MR->getKind()) {
796 case MemRegion::SymbolicRegionKind: {
797 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000798 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000799 QualType T = Sym->getType(getContext());
800 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000802 if (const PointerType *PT = T->getAs<PointerType>())
803 EleTy = PT->getPointeeType();
804 else
John McCall183700f2009-09-21 23:43:11 +0000805 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Ted Kremenek3bccf082009-07-11 00:58:27 +0000807 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
808 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000809 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000810 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000811 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000812 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000813 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000814 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000815 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
816 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000817 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000818 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000819
Ted Kremenek3bccf082009-07-11 00:58:27 +0000820 case MemRegion::ElementRegionKind: {
821 ER = cast<ElementRegion>(MR);
822 break;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenek3bccf082009-07-11 00:58:27 +0000825 // Not yet handled.
826 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000827 case MemRegion::StringRegionKind: {
828
829 }
830 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000831 case MemRegion::CompoundLiteralRegionKind:
832 case MemRegion::FieldRegionKind:
833 case MemRegion::ObjCObjectRegionKind:
834 case MemRegion::ObjCIvarRegionKind:
835 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Ted Kremenek3bccf082009-07-11 00:58:27 +0000837 case MemRegion::CodeTextRegionKind:
838 // Technically this can happen if people do funny things with casts.
839 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 case MemRegion::MemSpaceRegionKind:
842 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
843 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 case MemRegion::BEG_DECL_REGIONS:
846 case MemRegion::END_DECL_REGIONS:
847 case MemRegion::BEG_TYPED_REGIONS:
848 case MemRegion::END_TYPED_REGIONS:
849 assert(0 && "Infeasible region");
850 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000851 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000852
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000853 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000854 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000855
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000856 // For now, only support:
857 // (a) concrete integer indices that can easily be resolved
858 // (b) 0 + symbolic index
859 if (Base) {
860 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
861 // FIXME: Should use SValuator here.
862 SVal NewIdx =
863 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000864 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000865 const MemRegion* NewER =
866 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
867 ER->getSuperRegion(), getContext());
868 return ValMgr.makeLoc(NewER);
869 }
870 if (0 == Base->getValue()) {
871 const MemRegion* NewER =
872 MRMgr.getElementRegion(ER->getElementType(), R,
873 ER->getSuperRegion(), getContext());
874 return ValMgr.makeLoc(NewER);
875 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Ted Kremenek5dc27462009-03-03 02:51:43 +0000878 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000879}
880
Ted Kremenek9af46f52009-06-16 22:36:44 +0000881//===----------------------------------------------------------------------===//
882// Loading values from regions.
883//===----------------------------------------------------------------------===//
884
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000885Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
886 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000888 if (R->isBoundable())
889 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
890 if (TR->getValueType(getContext())->isUnionType())
891 return UnknownVal();
892
893 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
894}
895
Ted Kremeneka6275a52009-07-15 02:31:43 +0000896static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
897 RTy = Ctx.getCanonicalType(RTy);
898 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Ted Kremeneka6275a52009-07-15 02:31:43 +0000900 if (RTy == UsedTy)
901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
903
Ted Kremenek25c54572009-07-20 22:58:02 +0000904 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000905 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000906 // represents a reinterpretation.
907 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000908 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000909 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000910
911 return PUsedTy && PRTy &&
912 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000913 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000914 }
915
916 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000917}
918
Ted Kremenek0954cde2009-09-24 04:11:44 +0000919const ElementRegion *
920RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
921 ASTContext &Ctx = getContext();
922 SVal idx = ValMgr.makeZeroArrayIndex();
923 assert(!T.isNull());
924 return MRMgr.getElementRegion(T, idx, SR, Ctx);
925}
926
927
928
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000929SValuator::CastResult
930RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000931
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000932 assert(!isa<UnknownVal>(L) && "location unknown");
933 assert(!isa<UndefinedVal>(L) && "location undefined");
934
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000935 // FIXME: Is this even possible? Shouldn't this be treated as a null
936 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000937 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000938 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000939
Ted Kremenek67f28532009-06-17 22:02:04 +0000940 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000941
Zhongxing Xu91844122009-05-20 09:18:48 +0000942 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000943 // Example:
944 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000945 // char* p = alloca();
946 // read(p);
947 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000948 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000949 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Ted Kremenek0954cde2009-09-24 04:11:44 +0000951 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
952 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremenek968f0a62009-08-03 21:41:46 +0000954 if (isa<CodeTextRegion>(MR))
955 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000957 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
958 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000959 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000960 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000961
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000962 // FIXME: We should eventually handle funny addressing. e.g.:
963 //
964 // int x = ...;
965 // int *p = &x;
966 // char *q = (char*) p;
967 // char c = *q; // returns the first byte of 'x'.
968 //
969 // Such funny addressing will occur due to layering of regions.
970
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000971#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000972 ASTContext &Ctx = getContext();
973 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000974 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
975 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000976 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000977 assert(Ctx.getCanonicalType(RTy) ==
978 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +0000979 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000980#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000981
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000982 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000983 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000985 // FIXME: Handle unions.
986 if (RTy->isUnionType())
987 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000988
989 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000990 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000991
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000992 // FIXME: handle Vector types.
993 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000994 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000995
996 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000997 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000998
999 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001000 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek25c54572009-07-20 22:58:02 +00001002 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001003 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek9031dd72009-07-21 00:12:07 +00001005 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001006 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001007
Ted Kremenek451ac092009-08-06 04:50:20 +00001008 RegionBindings B = GetRegionBindings(state->getStore());
1009 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001010
1011 // Check if the region has a binding.
1012 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001013 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001014
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001015 // The location does not have a bound value. This means that it has
1016 // the value it had upon its creation and/or entry to the analyzed
1017 // function/method. These are either symbolic values or 'undefined'.
1018
Ted Kremenek356e9d62009-07-22 04:35:42 +00001019#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001020 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001021#else
1022 if (R->hasStackStorage()) {
1023#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001024 // All stack variables are considered to have undefined values
1025 // upon creation. All heap allocated blocks are considered to
1026 // have undefined values as well unless they are explicitly bound
1027 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001028 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001029 }
1030
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001031 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001032 return SValuator::CastResult(state,
1033 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001034}
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001036std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001037RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001038
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001039 if (const nonloc::LazyCompoundVal *V =
1040 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
1041 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001043 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1044 const std::pair<const GRState *, const MemRegion *> &X =
1045 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001047 if (X.first)
1048 return std::make_pair(X.first,
1049 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001050 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001051 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1052 const std::pair<const GRState *, const MemRegion *> &X =
1053 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001055 if (X.first)
1056 return std::make_pair(X.first,
1057 MRMgr.getFieldRegionWithSuper(FR, X.second));
1058 }
1059
1060 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1061}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001062
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001063SVal RegionStoreManager::RetrieveElement(const GRState* state,
1064 const ElementRegion* R) {
1065 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001066 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001067 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001068 return *V;
1069
Ted Kremenek921109a2009-07-01 23:19:52 +00001070 const MemRegion* superR = R->getSuperRegion();
1071
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001072 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001073 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001074 // FIXME: Handle loads from strings where the literal is treated as
1075 // an integer, e.g., *((unsigned int*)"hello")
1076 ASTContext &Ctx = getContext();
1077 QualType T = StrR->getValueType(Ctx)->getAs<ArrayType>()->getElementType();
1078 if (T != Ctx.getCanonicalType(R->getElementType()))
1079 return UnknownVal();
1080
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001081 const StringLiteral *Str = StrR->getStringLiteral();
1082 SVal Idx = R->getIndex();
1083 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1084 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001085 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001086 if (i > byteLength) {
1087 // Buffer overflow checking in GRExprEngine should handle this case,
1088 // but we shouldn't rely on it to not overflow here if that checking
1089 // is disabled.
1090 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001091 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001092 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001093 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001094 }
1095 }
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001097 // Special case: the current region represents a cast and it and the super
1098 // region both have pointer types or intptr_t types. If so, perform the
1099 // retrieve from the super region and appropriately "cast" the value.
1100 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001101 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001102 if (R->getIndex().isZeroConstant()) {
1103 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1104 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001105 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1106 QualType valTy = R->getValueType(Ctx);
1107 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1108 // Retrieve the value from the super region. This will be casted to
1109 // valTy when we return to 'Retrieve'.
1110 const SValuator::CastResult &cr = Retrieve(state,
1111 loc::MemRegionVal(superR),
1112 valTy);
1113 return cr.getSVal();
1114 }
1115 }
1116 }
1117 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001118
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001119 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001120 if (const SVal *V = B.lookup(superR)) {
1121 if (SymbolRef parentSym = V->getAsSymbol())
1122 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001123
1124 if (V->isUnknownOrUndef())
1125 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001126
1127 // Handle LazyCompoundVals for the immediate super region. Other cases
1128 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001129 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001130 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001132 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1133 return RetrieveElement(LCV->getState(), R);
1134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Ted Kremeneka6275a52009-07-15 02:31:43 +00001136 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001137 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001140 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001141}
1142
Mike Stump1eb44332009-09-09 15:08:12 +00001143SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001144 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001145
1146 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001147 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001148 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001149 return *V;
1150
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001151 QualType Ty = R->getValueType(getContext());
1152 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1153}
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001155SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1156 const TypedRegion *R,
1157 QualType Ty,
1158 const MemRegion *superR) {
1159
Mike Stump1eb44332009-09-09 15:08:12 +00001160 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001161 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001163 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001165 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001166 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001167 if (SymbolRef parentSym = D->getAsSymbol())
1168 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001170 if (D->isZeroConstant())
1171 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001173 if (D->isUnknown())
1174 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001176 assert(0 && "Unknown default value");
1177 }
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001179 // If our super region is a field or element itself, walk up the region
1180 // hierarchy to see if there is a default value installed in an ancestor.
1181 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1182 superR = cast<SubRegion>(superR)->getSuperRegion();
1183 continue;
1184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001186 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001187 }
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001189 // Lazy binding?
1190 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001191 const MemRegion *lazyBindingRegion = NULL;
1192 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001194 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001195 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001197 if (isa<ElementRegion>(R))
1198 return RetrieveElement(lazyBindingState,
1199 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001201 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001202 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001203 }
1204
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001205 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001207 if (isa<ElementRegion>(R)) {
1208 // Currently we don't reason specially about Clang-style vectors. Check
1209 // if superR is a vector and if so return Unknown.
1210 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1211 if (typedSuperR->getValueType(getContext())->isVectorType())
1212 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001213 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001216 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001219 // All other values are symbolic.
1220 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001221}
Mike Stump1eb44332009-09-09 15:08:12 +00001222
1223SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001224 const ObjCIvarRegion* R) {
1225
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001226 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001227 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001228
1229 if (const SVal* V = B.lookup(R))
1230 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001232 const MemRegion *superR = R->getSuperRegion();
1233
1234 // Check if the super region has a binding.
1235 if (const SVal *V = B.lookup(superR)) {
1236 if (SymbolRef parentSym = V->getAsSymbol())
1237 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001239 // Other cases: give up.
1240 return UnknownVal();
1241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Ted Kremenek25c54572009-07-20 22:58:02 +00001243 return RetrieveLazySymbol(state, R);
1244}
1245
Ted Kremenek9031dd72009-07-21 00:12:07 +00001246SVal RegionStoreManager::RetrieveVar(const GRState *state,
1247 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001248
Ted Kremenek9031dd72009-07-21 00:12:07 +00001249 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001250 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Ted Kremenek9031dd72009-07-21 00:12:07 +00001252 if (const SVal* V = B.lookup(R))
1253 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Ted Kremenek9031dd72009-07-21 00:12:07 +00001255 // Lazily derive a value for the VarRegion.
1256 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001257
Ted Kremenek9031dd72009-07-21 00:12:07 +00001258 if (R->hasGlobalsOrParametersStorage())
1259 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek9031dd72009-07-21 00:12:07 +00001261 return UndefinedVal();
1262}
1263
Mike Stump1eb44332009-09-09 15:08:12 +00001264SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001265 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Ted Kremenek25c54572009-07-20 22:58:02 +00001267 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001268
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001269 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001270 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001271}
1272
Mike Stump1eb44332009-09-09 15:08:12 +00001273SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1274 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001275 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001276 assert(T->isStructureType());
1277
Zhongxing Xub7507d12009-06-11 07:27:30 +00001278 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001279 RecordDecl* RD = RT->getDecl();
1280 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001281 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001282#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001283 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1284
Ted Kremenek67f28532009-06-17 22:02:04 +00001285 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1286 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001287 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001288
Douglas Gregore267ff32008-12-11 20:41:00 +00001289 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1290 FieldEnd = Fields.rend();
1291 Field != FieldEnd; ++Field) {
1292 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001293 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001294 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001295 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1296 }
1297
Zhongxing Xud91ee272009-06-23 09:02:15 +00001298 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001299#else
1300 return ValMgr.makeLazyCompoundVal(state, R);
1301#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001302}
1303
Ted Kremenek67f28532009-06-17 22:02:04 +00001304SVal RegionStoreManager::RetrieveArray(const GRState *state,
1305 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001306#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001307 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001308 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1309
1310 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001311 uint64_t size = CAT->getSize().getZExtValue();
1312 for (uint64_t i = 0; i < size; ++i) {
1313 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001314 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001315 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001316 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001317 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001318 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1319 }
1320
Zhongxing Xud91ee272009-06-23 09:02:15 +00001321 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001322#else
1323 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1324 return ValMgr.makeLazyCompoundVal(state, R);
1325#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001326}
1327
Ted Kremenek9af46f52009-06-16 22:36:44 +00001328//===----------------------------------------------------------------------===//
1329// Binding values to regions.
1330//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001331
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001332Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001333 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Ted Kremenek0964a062009-01-21 06:57:53 +00001335 if (isa<loc::MemRegionVal>(L))
1336 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Ted Kremenek0964a062009-01-21 06:57:53 +00001338 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001339 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001340 return RBFactory.Remove(B, R).getRoot();
1341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Ted Kremenek0964a062009-01-21 06:57:53 +00001343 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001344}
1345
Ted Kremenek67f28532009-06-17 22:02:04 +00001346const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001347 if (isa<loc::ConcreteInt>(L))
1348 return state;
1349
Ted Kremenek9af46f52009-06-16 22:36:44 +00001350 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001351 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Ted Kremenek9af46f52009-06-16 22:36:44 +00001353 // Check if the region is a struct region.
1354 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1355 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001356 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001358 // Special case: the current region represents a cast and it and the super
1359 // region both have pointer types or intptr_t types. If so, perform the
1360 // bind to the super region.
1361 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001362 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001363 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1364 if (ER->getIndex().isZeroConstant()) {
1365 if (const TypedRegion *superR =
1366 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1367 ASTContext &Ctx = getContext();
1368 QualType superTy = superR->getValueType(Ctx);
1369 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001370
1371 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001372 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001373 SValuator::CastResult cr =
1374 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001375 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1376 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001377 // For now, just invalidate the fields of the struct/union/class.
1378 // FIXME: Precisely handle the fields of the record.
1379 if (superTy->isRecordType())
1380 return InvalidateRegion(state, superR, NULL, 0);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001381 }
1382 }
1383 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001384 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1385 // Binding directly to a symbolic region should be treated as binding
1386 // to element 0.
1387 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001388 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001389 R = GetElementZeroRegion(SR, T);
1390 }
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001392 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001393 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001394 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001395}
1396
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001397const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1398 const VarDecl *VD,
1399 const LocationContext *LC,
1400 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001401
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001402 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001403 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001404
Ted Kremenek0964a062009-01-21 06:57:53 +00001405 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001406 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001407 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001408 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001409
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001410 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001411}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001412
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001413// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001414const GRState *
1415RegionStoreManager::BindCompoundLiteral(const GRState *state,
1416 const CompoundLiteralExpr* CL,
1417 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001419 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001420 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001421}
1422
Ted Kremenek67f28532009-06-17 22:02:04 +00001423const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001424 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001425 SVal Init) {
1426
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001427 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001428 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001429 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001430
Ted Kremenek46537392009-07-16 01:33:37 +00001431 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001432
1433 // Check if the init expr is a StringLiteral.
1434 if (isa<loc::MemRegionVal>(Init)) {
1435 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1436 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1437 const char* str = S->getStrData();
1438 unsigned len = S->getByteLength();
1439 unsigned j = 0;
1440
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001441 // Copy bytes from the string literal into the target array. Trailing bytes
1442 // in the array that are not covered by the string literal are initialized
1443 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001444 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001445 if (j >= len)
1446 break;
1447
Ted Kremenek46537392009-07-16 01:33:37 +00001448 SVal Idx = ValMgr.makeArrayIndex(i);
1449 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1450 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001451
Zhongxing Xud91ee272009-06-23 09:02:15 +00001452 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001453 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001454 }
1455
Ted Kremenek67f28532009-06-17 22:02:04 +00001456 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001457 }
1458
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001459 // Handle lazy compound values.
1460 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1461 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
1463 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001464 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001465 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001466 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Ted Kremenek46537392009-07-16 01:33:37 +00001468 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001469 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001470 if (VI == VE)
1471 break;
1472
Ted Kremenek46537392009-07-16 01:33:37 +00001473 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001474 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001475
1476 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001477 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001478 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001479 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001480 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001481 }
1482
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001483 // If the init list is shorter than the array length, set the array default
1484 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001485 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001486 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001487 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xu4f8c7e42009-10-09 02:18:31 +00001488 state = state->set<RegionDefaultValue>(R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001489 }
1490 }
1491
Ted Kremenek67f28532009-06-17 22:02:04 +00001492 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001493}
1494
Ted Kremenek67f28532009-06-17 22:02:04 +00001495const GRState *
1496RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1497 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Ted Kremenek67f28532009-06-17 22:02:04 +00001499 if (!Features.supportsFields())
1500 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001502 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001503 assert(T->isStructureType());
1504
Ted Kremenek6217b802009-07-29 21:53:49 +00001505 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001506 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001507
1508 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001509 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001510
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001511 // Handle lazy compound values.
1512 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1513 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Ted Kremenek67f28532009-06-17 22:02:04 +00001515 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1516 // Ignore them and kill the field values.
1517 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1518 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001519
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001520 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001521 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001522
1523 RecordDecl::field_iterator FI, FE;
1524
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001525 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001526
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001527 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001528 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001529
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001530 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001531 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001532
Ted Kremenekcf549592009-09-22 21:19:14 +00001533 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001534 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001535 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001536 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001537 else
1538 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001539 }
1540
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001541 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001542 if (FI != FE)
Zhongxing Xu4f8c7e42009-10-09 02:18:31 +00001543 state = state->set<RegionDefaultValue>(R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001544
Ted Kremenek67f28532009-06-17 22:02:04 +00001545 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001546}
1547
Ted Kremenek67f28532009-06-17 22:02:04 +00001548const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001549 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001550
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001551 // Set the default value of the struct region to "unknown".
1552 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001553
1554 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001555 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001556 RegionBindings B = GetRegionBindings(store);
1557 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001558 const MemRegion* R = I.getKey();
1559 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1560 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001561 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001562 }
1563
Ted Kremenek67f28532009-06-17 22:02:04 +00001564 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001565}
1566
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001567const GRState*
1568RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1569 const GRState *state,
1570 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001571
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001572 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001573 RegionBindings B = GetRegionBindings(state->getStore());
1574 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1575 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001576 state->get_context<RegionDefaultValue>();
1577
Mike Stump1eb44332009-09-09 15:08:12 +00001578 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001579 SubRegions(getRegionStoreSubRegionMap(state));
1580
Mike Stump1eb44332009-09-09 15:08:12 +00001581 // B and DVM are updated after the call to RemoveSubRegionBindings.
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001582 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001584 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1585 // results in a zero-copy algorithm.
1586 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1587}
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Ted Kremenek9af46f52009-06-16 22:36:44 +00001589//===----------------------------------------------------------------------===//
1590// State pruning.
1591//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenek451ac092009-08-06 04:50:20 +00001593namespace {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001594class VISIBILITY_HIDDEN RBDNode
1595 : public std::pair<const GRState*, const MemRegion *> {
Ted Kremenek451ac092009-08-06 04:50:20 +00001596public:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001597 RBDNode(const GRState *st, const MemRegion *r)
1598 : std::pair<const GRState*, const MemRegion*>(st, r) {}
1599
1600 const GRState *getState() const { return first; }
1601 const MemRegion *getRegion() const { return second; }
1602};
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001604enum VisitFlag { NotVisited = 0, VisitedFromSubRegion, VisitedFromSuperRegion };
1605
1606class RBDItem : public RBDNode {
1607private:
1608 const VisitFlag VF;
1609
1610public:
1611 RBDItem(const GRState *st, const MemRegion *r, VisitFlag vf)
1612 : RBDNode(st, r), VF(vf) {}
1613
1614 VisitFlag getVisitFlag() const { return VF; }
Ted Kremenek451ac092009-08-06 04:50:20 +00001615};
1616} // end anonymous namespace
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001617
Mike Stump1eb44332009-09-09 15:08:12 +00001618void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001619 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001620 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001621{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001622 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001623 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001624 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Ted Kremenek9af46f52009-06-16 22:36:44 +00001626 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001627 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001628 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001629
1630 // Do a pass over the regions in the store. For VarRegions we check if
1631 // the variable is still live and if so add it to the list of live roots.
1632 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001633 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001634
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001635 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001636 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001637 const MemRegion *R = I.getKey();
1638 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001639 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001640
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001641 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001642 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001643 I != E; ++I) {
1644 const MemRegion *R = I.getKey();
1645 IntermediateRoots.push_back(R);
1646 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001647
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001648 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001649 // real roots.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001650 llvm::SmallVector<RBDItem, 10> WorkList;
1651 llvm::DenseMap<const MemRegion*,unsigned> IntermediateVisited;
1652
Ted Kremenek9af46f52009-06-16 22:36:44 +00001653 while (!IntermediateRoots.empty()) {
1654 const MemRegion* R = IntermediateRoots.back();
1655 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001656
1657 unsigned &visited = IntermediateVisited[R];
1658 if (visited)
1659 continue;
1660 visited = 1;
1661
Ted Kremenek9af46f52009-06-16 22:36:44 +00001662 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001663 if (SymReaper.isLive(Loc, VR->getDecl()))
1664 WorkList.push_back(RBDItem(&state, VR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001665 continue;
1666 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001667
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001668 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 if (SymReaper.isLive(SR->getSymbol()))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001670 WorkList.push_back(RBDItem(&state, SR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001671 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001672 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001673
1674 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001675 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001676 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001677 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001678 }
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001680 // Enqueue the RegionRoots onto WorkList.
1681 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1682 E=RegionRoots.end(); I!=E; ++I) {
1683 WorkList.push_back(RBDItem(&state, *I, VisitedFromSuperRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001684 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001685 RegionRoots.clear();
1686
1687 // Process the worklist.
1688 typedef llvm::DenseMap<std::pair<const GRState*, const MemRegion*>, VisitFlag>
1689 VisitMap;
1690
1691 VisitMap Visited;
1692
1693 while (!WorkList.empty()) {
1694 RBDItem N = WorkList.back();
1695 WorkList.pop_back();
1696
1697 // Have we visited this node before?
1698 VisitFlag &VF = Visited[N];
1699 if (VF >= N.getVisitFlag())
1700 continue;
1701
1702 const MemRegion *R = N.getRegion();
1703 const GRState *state_N = N.getState();
1704
1705 // Enqueue subregions?
1706 if (N.getVisitFlag() == VisitedFromSuperRegion) {
1707 RegionStoreSubRegionMap *M;
1708
1709 if (&state == state_N)
1710 M = SubRegions.get();
1711 else {
1712 RegionStoreSubRegionMap *& SM = SC[state_N];
1713 if (!SM)
1714 SM = getRegionStoreSubRegionMap(state_N);
1715 M = SM;
1716 }
1717
1718 RegionStoreSubRegionMap::iterator I, E;
1719 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1720 WorkList.push_back(RBDItem(state_N, *I, VisitedFromSuperRegion));
1721 }
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001723 // At this point, if we have already visited this region before, we are
1724 // done.
1725 if (VF != NotVisited) {
1726 VF = N.getVisitFlag();
1727 continue;
1728 }
1729 VF = N.getVisitFlag();
1730
1731 // Enqueue the super region.
1732 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1733 const MemRegion *superR = SR->getSuperRegion();
1734 if (!isa<MemSpaceRegion>(superR)) {
1735 // If 'R' is a field or an element, we want to keep the bindings
1736 // for the other fields and elements around. The reason is that
1737 // pointer arithmetic can get us to the other fields or elements.
1738 VisitFlag NewVisit =
1739 isa<FieldRegion>(R) || isa<ElementRegion>(R) || isa<ObjCIvarRegion>(R)
1740 ? VisitedFromSuperRegion : VisitedFromSubRegion;
1741
1742 WorkList.push_back(RBDItem(state_N, superR, NewVisit));
1743 }
1744 }
1745
1746 // Mark the symbol for any live SymbolicRegion as "live". This means we
1747 // should continue to track that symbol.
1748 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1749 SymReaper.markLive(SymR->getSymbol());
1750
1751 Store store_N = state_N->getStore();
1752 RegionBindings B_N = GetRegionBindings(store_N);
1753
1754 // Get the data binding for R (if any).
1755 const SVal* Xptr = B_N.lookup(R);
1756
1757 // Check for lazy bindings.
1758 if (const nonloc::LazyCompoundVal *V =
1759 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1760
1761 const LazyCompoundValData *D = V->getCVData();
1762 WorkList.push_back(RBDItem(D->getState(), D->getRegion(),
1763 VisitedFromSuperRegion));
1764 }
1765 else {
1766 // No direct binding? Get the default binding for R (if any).
1767 if (!Xptr) {
1768 RegionDefaultBindings DVM_N = &state == state_N ? DVM
1769 : state_N->get<RegionDefaultValue>();
1770
1771 Xptr = DVM_N.lookup(R);
1772 }
1773
1774 // Direct or default binding?
1775 if (Xptr) {
1776 SVal X = *Xptr;
1777
1778 // Update the set of live symbols.
1779 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();
1780 SI!=SE;++SI)
1781 SymReaper.markLive(*SI);
1782
1783 // If X is a region, then add it to the worklist.
1784 if (const MemRegion *RX = X.getAsRegion())
1785 WorkList.push_back(RBDItem(state_N, RX, VisitedFromSuperRegion));
1786 }
1787 }
1788 }
1789
Ted Kremenek9af46f52009-06-16 22:36:44 +00001790 // We have now scanned the store, marking reachable regions and symbols
1791 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001792 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001793 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001794 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001795 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001796 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek9af46f52009-06-16 22:36:44 +00001797 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Ted Kremenek9af46f52009-06-16 22:36:44 +00001799 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001800 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Ted Kremenek9af46f52009-06-16 22:36:44 +00001802 // Mark all non-live symbols that this region references as dead.
1803 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1804 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806 SVal X = I.getData();
1807 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001808 for (; SI != SE; ++SI)
1809 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001810 }
Mike Stump1eb44332009-09-09 15:08:12 +00001811
1812 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001813 RegionDefaultBindings NewDVM = DVM;
Mike Stump1eb44332009-09-09 15:08:12 +00001814 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001815 state.get_context<RegionDefaultValue>();
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Ted Kremenek451ac092009-08-06 04:50:20 +00001817 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001818 I != E; ++I) {
1819 const MemRegion *R = I.getKey();
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Ted Kremenek093569c2009-08-02 05:00:15 +00001821 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001822 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek093569c2009-08-02 05:00:15 +00001823 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Ted Kremenek093569c2009-08-02 05:00:15 +00001825 // Remove this dead region.
1826 NewDVM = DVMFactory.Remove(NewDVM, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Ted Kremenek093569c2009-08-02 05:00:15 +00001828 // Mark all non-live symbols that this region references as dead.
1829 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1830 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek093569c2009-08-02 05:00:15 +00001832 SVal X = I.getData();
1833 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1834 for (; SI != SE; ++SI)
1835 SymReaper.maybeDead(*SI);
1836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001838 // Write the store back.
1839 state.setStore(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Ted Kremenek093569c2009-08-02 05:00:15 +00001841 // Write the updated default bindings back.
1842 // FIXME: Right now this involves a fetching of a persistent state.
1843 // We can do better.
1844 if (DVM != NewDVM)
1845 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001846}
1847
1848//===----------------------------------------------------------------------===//
1849// Utility methods.
1850//===----------------------------------------------------------------------===//
1851
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001852void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001853 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001854 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001855 OS << "Store (direct bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Ted Kremenek451ac092009-08-06 04:50:20 +00001857 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001858 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001859}