blob: da04d68dcf04dd70951e278709fe96e236405d96 [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;
Zhongxing Xu17892752008-10-08 02:50:44 +0000171public:
Mike Stump1eb44332009-09-09 15:08:12 +0000172 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000173 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000174 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000175 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000176
177 virtual ~RegionStoreManager() {}
178
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000179 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000181 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
183
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000184 /// getDefaultBinding - Returns an SVal* representing an optional default
185 /// binding associated with a region and its subregions.
186 Optional<SVal> getDefaultBinding(const GRState *state, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000188 /// getLValueString - Returns an SVal representing the lvalue of a
189 /// StringLiteral. Within RegionStore a StringLiteral has an
190 /// associated StringRegion, and the lvalue of a StringLiteral is
191 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000192 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000193
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000194 /// getLValueCompoundLiteral - Returns an SVal representing the
195 /// lvalue of a compound literal. Within RegionStore a compound
196 /// literal has an associated region, and the lvalue of the
197 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000198 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000199
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000200 /// getLValueVar - Returns an SVal that represents the lvalue of a
201 /// variable. Within RegionStore a variable has an associated
202 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000203 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
204 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremenek67f28532009-06-17 22:02:04 +0000206 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000207
Ted Kremenek67f28532009-06-17 22:02:04 +0000208 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenek67f28532009-06-17 22:02:04 +0000210 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000211
Ted Kremenek67f28532009-06-17 22:02:04 +0000212 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000213 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000214
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000215
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000216 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
217 /// type. 'Array' represents the lvalue of the array being decayed
218 /// to a pointer, and the returned SVal represents the decayed
219 /// version of that lvalue (i.e., a pointer to the first element of
220 /// the array). This is called by GRExprEngine when evaluating
221 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000222 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000223
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000224 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000225 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000226
Mike Stump1eb44332009-09-09 15:08:12 +0000227 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000228 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000229 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000230
Ted Kremenek67f28532009-06-17 22:02:04 +0000231 //===-------------------------------------------------------------------===//
232 // Binding values to regions.
233 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000234
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000235 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
236 const Expr *E, unsigned Count);
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000238private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000239 void RemoveSubRegionBindings(RegionBindings &B,
240 RegionDefaultBindings &DVM,
241 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000242 const MemRegion *R,
243 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
245public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000246 const GRState *Bind(const GRState *state, Loc LV, SVal V);
247
248 const GRState *BindCompoundLiteral(const GRState *state,
249 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000251 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
252 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000253
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000254 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
255 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000256 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000257 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000258
Ted Kremenek67f28532009-06-17 22:02:04 +0000259 /// BindStruct - Bind a compound value to a structure.
260 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Ted Kremenek67f28532009-06-17 22:02:04 +0000262 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000263
264 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek67f28532009-06-17 22:02:04 +0000265 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
266
267 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
268
269 Store Remove(Store store, Loc LV);
270
271 //===------------------------------------------------------------------===//
272 // Loading values from regions.
273 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek67f28532009-06-17 22:02:04 +0000275 /// The high level logic for this method is this:
276 /// Retrieve (L)
277 /// if L has binding
278 /// return L's binding
279 /// else if L is in killset
280 /// return unknown
281 /// else
282 /// if L is on stack or heap
283 /// return undefined
284 /// else
285 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000286 SValuator::CastResult Retrieve(const GRState *state, Loc L,
287 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000288
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000289 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000290
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000291 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000293 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek9031dd72009-07-21 00:12:07 +0000295 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Ted Kremenek25c54572009-07-20 22:58:02 +0000297 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000299 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
300 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenek67f28532009-06-17 22:02:04 +0000302 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000303 /// struct copy:
304 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000305 /// x = y;
306 /// y's value is retrieved by this method.
307 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek67f28532009-06-17 22:02:04 +0000309 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000310
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000311 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000312 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000314 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
315 const GRState *state,
316 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000317
Ted Kremenek0954cde2009-09-24 04:11:44 +0000318 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
319 QualType T);
320
Ted Kremenek67f28532009-06-17 22:02:04 +0000321 //===------------------------------------------------------------------===//
322 // State pruning.
323 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek67f28532009-06-17 22:02:04 +0000325 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
326 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000327 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000328 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
329
330 //===------------------------------------------------------------------===//
331 // Region "extents".
332 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek67f28532009-06-17 22:02:04 +0000334 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
335 SVal getSizeInElements(const GRState *state, const MemRegion* R);
336
337 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000338 // Utility methods.
339 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek451ac092009-08-06 04:50:20 +0000341 static inline RegionBindings GetRegionBindings(Store store) {
342 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000343 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000344
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000345 void print(Store store, llvm::raw_ostream& Out, const char* nl,
346 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000347
348 void iterBindings(Store store, BindingsHandler& f) {
349 // FIXME: Implement.
350 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000351
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 // FIXME: Remove.
353 BasicValueFactory& getBasicVals() {
354 return StateMgr.getBasicVals();
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000358 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000359};
360
361} // end anonymous namespace
362
Ted Kremenek9af46f52009-06-16 22:36:44 +0000363//===----------------------------------------------------------------------===//
364// RegionStore creation.
365//===----------------------------------------------------------------------===//
366
367StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
368 RegionStoreFeatures F = maximal_features_tag();
369 return new RegionStoreManager(StMgr, F);
370}
371
372StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
373 RegionStoreFeatures F = minimal_features_tag();
374 F.enableFields(true);
375 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000376}
377
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000378void
379RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000380 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000381 const MemRegion *superR = R->getSuperRegion();
382 if (add(superR, R))
383 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000384 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000385}
386
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000387RegionStoreSubRegionMap*
388RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000389 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000390 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000392 llvm::SmallVector<const SubRegion*, 10> WL;
393
Ted Kremenek451ac092009-08-06 04:50:20 +0000394 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000395 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
396 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Ted Kremenek451ac092009-08-06 04:50:20 +0000398 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
399 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000400 I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000401 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
402 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000403
Mike Stump1eb44332009-09-09 15:08:12 +0000404 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000405 // don't have direct bindings but are super regions of those that do.
406 while (!WL.empty()) {
407 const SubRegion *R = WL.back();
408 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000409 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000410 }
411
Ted Kremenek14453bf2009-03-03 19:02:42 +0000412 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000413}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000414
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000415SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
416 return getRegionStoreSubRegionMap(state);
417}
418
Ted Kremenek9af46f52009-06-16 22:36:44 +0000419//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000420// Binding invalidation.
421//===----------------------------------------------------------------------===//
422
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000423void
Ted Kremenek451ac092009-08-06 04:50:20 +0000424RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
425 RegionDefaultBindings &DVM,
426 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000427 const MemRegion *R,
428 RegionStoreSubRegionMap &M) {
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000430 RegionStoreSubRegionMap::iterator I, E;
431
432 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000433 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000435 B = RBFactory.Remove(B, R);
436 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437}
438
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000439const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
440 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000441 const Expr *Ex,
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000442 unsigned Count) {
443 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445 // Strip away casts.
446 R = R->getBaseRegion();
447
Ted Kremenek87806792009-09-27 20:45:21 +0000448 // Get the mapping of regions -> subregions.
449 llvm::OwningPtr<RegionStoreSubRegionMap>
450 SubRegions(getRegionStoreSubRegionMap(state));
451
452 RegionBindings B = GetRegionBindings(state->getStore());
453 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
454 RegionDefaultBindings::Factory &DVMFactory =
455 state->get_context<RegionDefaultValue>();
456
457 llvm::DenseMap<const MemRegion *, unsigned> Visited;
458 llvm::SmallVector<const MemRegion *, 10> WorkList;
459 WorkList.push_back(R);
460
461 while (!WorkList.empty()) {
462 R = WorkList.back();
463 WorkList.pop_back();
464
465 // Have we visited this region before?
466 unsigned &visited = Visited[R];
467 if (visited)
468 continue;
469 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek87806792009-09-27 20:45:21 +0000471 // Add subregions to work list.
472 RegionStoreSubRegionMap::iterator I, E;
473 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
474 WorkList.push_back(*I);
475
476 // Handle region.
477 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
478 isa<ObjCObjectRegion>(R)) {
479 // Invalidate the region by setting its default value to
480 // conjured symbol. The type of the symbol is irrelavant.
481 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
482 Count);
483 DVM = DVMFactory.Add(DVM, R, V);
484 continue;
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Ted Kremenek87806792009-09-27 20:45:21 +0000487 if (!R->isBoundable())
488 continue;
489
490 const TypedRegion *TR = cast<TypedRegion>(R);
491 QualType T = TR->getValueType(Ctx);
492
493 if (const RecordType *RT = T->getAsStructureType()) {
494 // FIXME: handle structs with default region value.
495 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
496
497 // No record definition. There is nothing we can do.
498 if (!RD)
499 continue;
500
501 // Invalidate the region by setting its default value to
502 // conjured symbol. The type of the symbol is irrelavant.
503 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
504 Count);
505 DVM = DVMFactory.Add(DVM, R, V);
506 continue;
507 }
508
509 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
510 // Set the default value of the array to conjured symbol.
511 DefinedOrUnknownSVal V =
512 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
513 DVM = DVMFactory.Add(DVM, R, V);
514 continue;
515 }
516
Ted Kremenek389c44c2009-09-29 03:12:50 +0000517 // FIXME: Special case FieldRegion/ElementRegion for more
518 // efficient invalidation. We don't need to conjure symbols for
519 // these regions in all cases.
520
Ted Kremenek87806792009-09-27 20:45:21 +0000521 // Get the old binding. Is it a region? If so, add it to the worklist.
522 if (const SVal *OldV = B.lookup(R)) {
523 if (const MemRegion *RV = OldV->getAsRegion())
524 WorkList.push_back(RV);
525 }
526
Ted Kremenek389c44c2009-09-29 03:12:50 +0000527 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000528 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
529 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
530 B = RBFactory.Add(B, R, V);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000531 }
532
Ted Kremenek87806792009-09-27 20:45:21 +0000533 // Create a new state with the updated bindings.
534 return state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000535}
536
537//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000538// getLValueXXX methods.
539//===----------------------------------------------------------------------===//
540
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000541/// getLValueString - Returns an SVal representing the lvalue of a
542/// StringLiteral. Within RegionStore a StringLiteral has an
543/// associated StringRegion, and the lvalue of a StringLiteral is the
544/// lvalue of that region.
Mike Stump1eb44332009-09-09 15:08:12 +0000545SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000546 const StringLiteral* S) {
547 return loc::MemRegionVal(MRMgr.getStringRegion(S));
548}
549
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000550/// getLValueVar - Returns an SVal that represents the lvalue of a
551/// variable. Within RegionStore a variable has an associated
552/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000553SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
554 const LocationContext *LC) {
555 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000556}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000557
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000558/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
559/// of a compound literal. Within RegionStore a compound literal
560/// has an associated region, and the lvalue of the compound literal
561/// is the lvalue of that region.
562SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000563RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Mike Stump1eb44332009-09-09 15:08:12 +0000564 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000565 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
566}
567
Ted Kremenek67f28532009-06-17 22:02:04 +0000568SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000569 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000570 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000571}
572
Ted Kremenek67f28532009-06-17 22:02:04 +0000573SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000574 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000575 return getLValueFieldOrIvar(St, Base, D);
576}
577
Ted Kremenek67f28532009-06-17 22:02:04 +0000578SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000579 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000580 if (Base.isUnknownOrUndef())
581 return Base;
582
583 Loc BaseL = cast<Loc>(Base);
584 const MemRegion* BaseR = 0;
585
586 switch (BaseL.getSubKind()) {
587 case loc::MemRegionKind:
588 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
589 break;
590
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000591 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592 // These are anormal cases. Flag an undefined value.
593 return UndefinedVal();
594
595 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000596 // While these seem funny, this can happen through casts.
597 // FIXME: What we should return is the field offset. For example,
598 // add the field offset to the integer value. That way funny things
599 // like this work properly: &(((struct foo *) 0xa)->f)
600 return Base;
601
602 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000603 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000604 return Base;
605 }
Mike Stump1eb44332009-09-09 15:08:12 +0000606
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000607 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
608 // of FieldDecl.
609 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
610 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000611
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000612 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000613}
614
Ted Kremenek67f28532009-06-17 22:02:04 +0000615SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000616 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000617 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000618
Ted Kremenekde7ec632009-03-09 22:44:49 +0000619 // If the base is an unknown or undefined value, just return it back.
620 // FIXME: For absolute pointer addresses, we just return that value back as
621 // well, although in reality we should return the offset added to that
622 // value.
623 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000624 return Base;
625
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000626 // Only handle integer offsets... for now.
627 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000628 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000629
Zhongxing Xuce760782009-05-09 13:20:07 +0000630 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000631
632 // Pointer of any type can be cast and used as array base.
633 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek46537392009-07-16 01:33:37 +0000635 // Convert the offset to the appropriate size and signedness.
636 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000638 if (!ElemR) {
639 //
640 // If the base region is not an ElementRegion, create one.
641 // This can happen in the following example:
642 //
643 // char *p = __builtin_alloc(10);
644 // p[1] = 8;
645 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000646 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000647 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000648 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000649 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000652 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000654 if (!isa<nonloc::ConcreteInt>(BaseIdx))
655 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000657 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
658 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
659 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000660
Ted Kremenek46537392009-07-16 01:33:37 +0000661 // Compute the new index.
662 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremenek46537392009-07-16 01:33:37 +0000664 // Construct the new ElementRegion.
665 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000666 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000667 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000668}
669
Ted Kremenek9af46f52009-06-16 22:36:44 +0000670//===----------------------------------------------------------------------===//
671// Extents for regions.
672//===----------------------------------------------------------------------===//
673
Ted Kremenek67f28532009-06-17 22:02:04 +0000674SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000675 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000677 switch (R->getKind()) {
678 case MemRegion::MemSpaceRegionKind:
679 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000680 return UnknownVal();
681
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000682 case MemRegion::CodeTextRegionKind:
683 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000684 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000685
686 // Not yet handled.
687 case MemRegion::AllocaRegionKind:
688 case MemRegion::CompoundLiteralRegionKind:
689 case MemRegion::ElementRegionKind:
690 case MemRegion::FieldRegionKind:
691 case MemRegion::ObjCIvarRegionKind:
692 case MemRegion::ObjCObjectRegionKind:
693 case MemRegion::SymbolicRegionKind:
694 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000696 case MemRegion::StringRegionKind: {
697 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000698 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000699 // operations with signed indices.
700 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000701 }
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703 case MemRegion::VarRegionKind: {
704 const VarRegion* VR = cast<VarRegion>(R);
705 // Get the type of the variable.
706 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000708 // FIXME: Handle variable-length arrays.
709 if (isa<VariableArrayType>(T))
710 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000712 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
713 // return the size as signed integer.
714 return ValMgr.makeIntVal(CAT->getSize(), false);
715 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000716
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717 // Clients can use ordinary variables as if they were arrays. These
718 // essentially are arrays of size 1.
719 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000722 case MemRegion::BEG_DECL_REGIONS:
723 case MemRegion::END_DECL_REGIONS:
724 case MemRegion::BEG_TYPED_REGIONS:
725 case MemRegion::END_TYPED_REGIONS:
726 assert(0 && "Infeasible region");
727 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000730 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000731 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000732}
733
Ted Kremenek67f28532009-06-17 22:02:04 +0000734const GRState *RegionStoreManager::setExtent(const GRState *state,
735 const MemRegion *region,
736 SVal extent) {
737 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000738}
739
740//===----------------------------------------------------------------------===//
741// Location and region casting.
742//===----------------------------------------------------------------------===//
743
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000744/// ArrayToPointer - Emulates the "decay" of an array to a pointer
745/// type. 'Array' represents the lvalue of the array being decayed
746/// to a pointer, and the returned SVal represents the decayed
747/// version of that lvalue (i.e., a pointer to the first element of
748/// the array). This is called by GRExprEngine when evaluating casts
749/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000750SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000751 if (!isa<loc::MemRegionVal>(Array))
752 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Ted Kremenekabb042f2008-12-13 19:24:37 +0000754 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
755 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000757 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000758 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000760 // Strip off typedefs from the ArrayRegion's ValueType.
761 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000762 ArrayType *AT = cast<ArrayType>(T);
763 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek75185b52009-07-16 00:00:11 +0000765 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
766 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000767
768 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000769}
770
Ted Kremenek9af46f52009-06-16 22:36:44 +0000771//===----------------------------------------------------------------------===//
772// Pointer arithmetic.
773//===----------------------------------------------------------------------===//
774
Mike Stump1eb44332009-09-09 15:08:12 +0000775SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000776 BinaryOperator::Opcode Op, Loc L, NonLoc R,
777 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000778 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000779 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000780 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000781
Zhongxing Xua1718c72009-04-03 07:33:13 +0000782 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000783 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000784
Ted Kremenek3bccf082009-07-11 00:58:27 +0000785 switch (MR->getKind()) {
786 case MemRegion::SymbolicRegionKind: {
787 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000788 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000789 QualType T = Sym->getType(getContext());
790 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000792 if (const PointerType *PT = T->getAs<PointerType>())
793 EleTy = PT->getPointeeType();
794 else
John McCall183700f2009-09-21 23:43:11 +0000795 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Ted Kremenek3bccf082009-07-11 00:58:27 +0000797 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
798 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000799 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000800 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000801 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000802 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000803 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000804 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000805 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
806 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000807 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000808 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000809
Ted Kremenek3bccf082009-07-11 00:58:27 +0000810 case MemRegion::ElementRegionKind: {
811 ER = cast<ElementRegion>(MR);
812 break;
813 }
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek3bccf082009-07-11 00:58:27 +0000815 // Not yet handled.
816 case MemRegion::VarRegionKind:
817 case MemRegion::StringRegionKind:
818 case MemRegion::CompoundLiteralRegionKind:
819 case MemRegion::FieldRegionKind:
820 case MemRegion::ObjCObjectRegionKind:
821 case MemRegion::ObjCIvarRegionKind:
822 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Ted Kremenek3bccf082009-07-11 00:58:27 +0000824 case MemRegion::CodeTextRegionKind:
825 // Technically this can happen if people do funny things with casts.
826 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Ted Kremenek3bccf082009-07-11 00:58:27 +0000828 case MemRegion::MemSpaceRegionKind:
829 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
830 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek3bccf082009-07-11 00:58:27 +0000832 case MemRegion::BEG_DECL_REGIONS:
833 case MemRegion::END_DECL_REGIONS:
834 case MemRegion::BEG_TYPED_REGIONS:
835 case MemRegion::END_TYPED_REGIONS:
836 assert(0 && "Infeasible region");
837 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000838 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000839
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000840 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000841 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
842 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
843
844 // Only support concrete integer indexes for now.
845 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000846 // FIXME: Should use SValuator here.
847 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
848 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000849 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000850 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
851 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000852 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000853 }
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Ted Kremenek5dc27462009-03-03 02:51:43 +0000855 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000856}
857
Ted Kremenek9af46f52009-06-16 22:36:44 +0000858//===----------------------------------------------------------------------===//
859// Loading values from regions.
860//===----------------------------------------------------------------------===//
861
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000862Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
863 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000865 if (R->isBoundable())
866 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
867 if (TR->getValueType(getContext())->isUnionType())
868 return UnknownVal();
869
870 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
871}
872
Ted Kremeneka6275a52009-07-15 02:31:43 +0000873static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
874 RTy = Ctx.getCanonicalType(RTy);
875 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Ted Kremeneka6275a52009-07-15 02:31:43 +0000877 if (RTy == UsedTy)
878 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000879
880
Ted Kremenek25c54572009-07-20 22:58:02 +0000881 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000882 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000883 // represents a reinterpretation.
884 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000885 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000886 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000887
888 return PUsedTy && PRTy &&
889 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000890 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000891 }
892
893 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000894}
895
Ted Kremenek0954cde2009-09-24 04:11:44 +0000896const ElementRegion *
897RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
898 ASTContext &Ctx = getContext();
899 SVal idx = ValMgr.makeZeroArrayIndex();
900 assert(!T.isNull());
901 return MRMgr.getElementRegion(T, idx, SR, Ctx);
902}
903
904
905
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000906SValuator::CastResult
907RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000908
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000909 assert(!isa<UnknownVal>(L) && "location unknown");
910 assert(!isa<UndefinedVal>(L) && "location undefined");
911
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000912 // FIXME: Is this even possible? Shouldn't this be treated as a null
913 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000914 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000915 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000916
Ted Kremenek67f28532009-06-17 22:02:04 +0000917 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000918
Zhongxing Xu91844122009-05-20 09:18:48 +0000919 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000920 // Example:
921 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000922 // char* p = alloca();
923 // read(p);
924 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000925 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000926 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Ted Kremenek0954cde2009-09-24 04:11:44 +0000928 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
929 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenek968f0a62009-08-03 21:41:46 +0000931 if (isa<CodeTextRegion>(MR))
932 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000934 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
935 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000936 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000937 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000938
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000939 // FIXME: We should eventually handle funny addressing. e.g.:
940 //
941 // int x = ...;
942 // int *p = &x;
943 // char *q = (char*) p;
944 // char c = *q; // returns the first byte of 'x'.
945 //
946 // Such funny addressing will occur due to layering of regions.
947
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000948#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000949 ASTContext &Ctx = getContext();
950 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000951 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
952 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000953 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000954 assert(Ctx.getCanonicalType(RTy) ==
955 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +0000956 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000957#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000958
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000959 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000960 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000962 // FIXME: Handle unions.
963 if (RTy->isUnionType())
964 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000965
966 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000967 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000968
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000969 // FIXME: handle Vector types.
970 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000971 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000972
973 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000974 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000975
976 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000977 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Ted Kremenek25c54572009-07-20 22:58:02 +0000979 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000980 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Ted Kremenek9031dd72009-07-21 00:12:07 +0000982 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000983 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000984
Ted Kremenek451ac092009-08-06 04:50:20 +0000985 RegionBindings B = GetRegionBindings(state->getStore());
986 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000987
988 // Check if the region has a binding.
989 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000990 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000991
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000992 // The location does not have a bound value. This means that it has
993 // the value it had upon its creation and/or entry to the analyzed
994 // function/method. These are either symbolic values or 'undefined'.
995
Ted Kremenek356e9d62009-07-22 04:35:42 +0000996#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000997 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000998#else
999 if (R->hasStackStorage()) {
1000#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001001 // All stack variables are considered to have undefined values
1002 // upon creation. All heap allocated blocks are considered to
1003 // have undefined values as well unless they are explicitly bound
1004 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001005 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001006 }
1007
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001008 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001009 return SValuator::CastResult(state,
1010 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001011}
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001013std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001014RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001015
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001016 if (const nonloc::LazyCompoundVal *V =
1017 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
1018 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001020 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1021 const std::pair<const GRState *, const MemRegion *> &X =
1022 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001024 if (X.first)
1025 return std::make_pair(X.first,
1026 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001027 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001028 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1029 const std::pair<const GRState *, const MemRegion *> &X =
1030 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001032 if (X.first)
1033 return std::make_pair(X.first,
1034 MRMgr.getFieldRegionWithSuper(FR, X.second));
1035 }
1036
1037 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1038}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001039
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001040SVal RegionStoreManager::RetrieveElement(const GRState* state,
1041 const ElementRegion* R) {
1042 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001043 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001044 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001045 return *V;
1046
Ted Kremenek921109a2009-07-01 23:19:52 +00001047 const MemRegion* superR = R->getSuperRegion();
1048
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001049 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001050 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001051 const StringLiteral *Str = StrR->getStringLiteral();
1052 SVal Idx = R->getIndex();
1053 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1054 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001055 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001056 if (i > byteLength) {
1057 // Buffer overflow checking in GRExprEngine should handle this case,
1058 // but we shouldn't rely on it to not overflow here if that checking
1059 // is disabled.
1060 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001061 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001062 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001063 return ValMgr.makeIntVal(c, getContext().CharTy);
1064 }
1065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001067 // Special case: the current region represents a cast and it and the super
1068 // region both have pointer types or intptr_t types. If so, perform the
1069 // retrieve from the super region and appropriately "cast" the value.
1070 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001071 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001072 if (R->getIndex().isZeroConstant()) {
1073 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1074 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001075 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1076 QualType valTy = R->getValueType(Ctx);
1077 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1078 // Retrieve the value from the super region. This will be casted to
1079 // valTy when we return to 'Retrieve'.
1080 const SValuator::CastResult &cr = Retrieve(state,
1081 loc::MemRegionVal(superR),
1082 valTy);
1083 return cr.getSVal();
1084 }
1085 }
1086 }
1087 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001088
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001089 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001090 if (const SVal *V = B.lookup(superR)) {
1091 if (SymbolRef parentSym = V->getAsSymbol())
1092 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001093
1094 if (V->isUnknownOrUndef())
1095 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001096
1097 // Handle LazyCompoundVals for the immediate super region. Other cases
1098 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001099 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001100 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001102 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1103 return RetrieveElement(LCV->getState(), R);
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremeneka6275a52009-07-15 02:31:43 +00001106 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001107 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001110 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001111}
1112
Mike Stump1eb44332009-09-09 15:08:12 +00001113SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001114 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001115
1116 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001117 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001118 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001119 return *V;
1120
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001121 QualType Ty = R->getValueType(getContext());
1122 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1123}
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001125SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1126 const TypedRegion *R,
1127 QualType Ty,
1128 const MemRegion *superR) {
1129
Mike Stump1eb44332009-09-09 15:08:12 +00001130 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001131 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001133 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001135 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001136 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001137 if (SymbolRef parentSym = D->getAsSymbol())
1138 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001140 if (D->isZeroConstant())
1141 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001143 if (D->isUnknown())
1144 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001146 assert(0 && "Unknown default value");
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001149 // If our super region is a field or element itself, walk up the region
1150 // hierarchy to see if there is a default value installed in an ancestor.
1151 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1152 superR = cast<SubRegion>(superR)->getSuperRegion();
1153 continue;
1154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001156 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001159 // Lazy binding?
1160 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001161 const MemRegion *lazyBindingRegion = NULL;
1162 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001164 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001165 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001167 if (isa<ElementRegion>(R))
1168 return RetrieveElement(lazyBindingState,
1169 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001171 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001172 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001173 }
1174
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001175 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001177 if (isa<ElementRegion>(R)) {
1178 // Currently we don't reason specially about Clang-style vectors. Check
1179 // if superR is a vector and if so return Unknown.
1180 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1181 if (typedSuperR->getValueType(getContext())->isVectorType())
1182 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001183 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001186 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001187 }
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001189 // All other values are symbolic.
1190 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001191}
Mike Stump1eb44332009-09-09 15:08:12 +00001192
1193SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001194 const ObjCIvarRegion* R) {
1195
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001196 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001197 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001198
1199 if (const SVal* V = B.lookup(R))
1200 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001202 const MemRegion *superR = R->getSuperRegion();
1203
1204 // Check if the super region has a binding.
1205 if (const SVal *V = B.lookup(superR)) {
1206 if (SymbolRef parentSym = V->getAsSymbol())
1207 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001209 // Other cases: give up.
1210 return UnknownVal();
1211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenek25c54572009-07-20 22:58:02 +00001213 return RetrieveLazySymbol(state, R);
1214}
1215
Ted Kremenek9031dd72009-07-21 00:12:07 +00001216SVal RegionStoreManager::RetrieveVar(const GRState *state,
1217 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremenek9031dd72009-07-21 00:12:07 +00001219 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001220 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek9031dd72009-07-21 00:12:07 +00001222 if (const SVal* V = B.lookup(R))
1223 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek9031dd72009-07-21 00:12:07 +00001225 // Lazily derive a value for the VarRegion.
1226 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Ted Kremenek9031dd72009-07-21 00:12:07 +00001228 if (R->hasGlobalsOrParametersStorage())
1229 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Ted Kremenek9031dd72009-07-21 00:12:07 +00001231 return UndefinedVal();
1232}
1233
Mike Stump1eb44332009-09-09 15:08:12 +00001234SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001235 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Ted Kremenek25c54572009-07-20 22:58:02 +00001237 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001238
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001239 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001240 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001241}
1242
Mike Stump1eb44332009-09-09 15:08:12 +00001243SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1244 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001245 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001246 assert(T->isStructureType());
1247
Zhongxing Xub7507d12009-06-11 07:27:30 +00001248 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001249 RecordDecl* RD = RT->getDecl();
1250 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001251 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001252#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001253 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1254
Ted Kremenek67f28532009-06-17 22:02:04 +00001255 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1256 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001257 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001258
Douglas Gregore267ff32008-12-11 20:41:00 +00001259 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1260 FieldEnd = Fields.rend();
1261 Field != FieldEnd; ++Field) {
1262 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001263 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001264 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001265 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1266 }
1267
Zhongxing Xud91ee272009-06-23 09:02:15 +00001268 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001269#else
1270 return ValMgr.makeLazyCompoundVal(state, R);
1271#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001272}
1273
Ted Kremenek67f28532009-06-17 22:02:04 +00001274SVal RegionStoreManager::RetrieveArray(const GRState *state,
1275 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001276#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001277 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001278 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1279
1280 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001281 uint64_t size = CAT->getSize().getZExtValue();
1282 for (uint64_t i = 0; i < size; ++i) {
1283 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001284 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001285 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001286 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001287 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001288 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1289 }
1290
Zhongxing Xud91ee272009-06-23 09:02:15 +00001291 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001292#else
1293 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1294 return ValMgr.makeLazyCompoundVal(state, R);
1295#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001296}
1297
Ted Kremenek9af46f52009-06-16 22:36:44 +00001298//===----------------------------------------------------------------------===//
1299// Binding values to regions.
1300//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001301
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001302Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001303 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Ted Kremenek0964a062009-01-21 06:57:53 +00001305 if (isa<loc::MemRegionVal>(L))
1306 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Ted Kremenek0964a062009-01-21 06:57:53 +00001308 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001309 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001310 return RBFactory.Remove(B, R).getRoot();
1311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Ted Kremenek0964a062009-01-21 06:57:53 +00001313 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001314}
1315
Ted Kremenek67f28532009-06-17 22:02:04 +00001316const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001317 if (isa<loc::ConcreteInt>(L))
1318 return state;
1319
Ted Kremenek9af46f52009-06-16 22:36:44 +00001320 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001321 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Ted Kremenek9af46f52009-06-16 22:36:44 +00001323 // Check if the region is a struct region.
1324 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1325 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001326 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001328 // Special case: the current region represents a cast and it and the super
1329 // region both have pointer types or intptr_t types. If so, perform the
1330 // bind to the super region.
1331 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001332 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001333 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1334 if (ER->getIndex().isZeroConstant()) {
1335 if (const TypedRegion *superR =
1336 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1337 ASTContext &Ctx = getContext();
1338 QualType superTy = superR->getValueType(Ctx);
1339 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001340
1341 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001342 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001343 SValuator::CastResult cr =
1344 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001345 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1346 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001347 // For now, just invalidate the fields of the struct/union/class.
1348 // FIXME: Precisely handle the fields of the record.
1349 if (superTy->isRecordType())
1350 return InvalidateRegion(state, superR, NULL, 0);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001351 }
1352 }
1353 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001354 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1355 // Binding directly to a symbolic region should be treated as binding
1356 // to element 0.
1357 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001358 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001359 R = GetElementZeroRegion(SR, T);
1360 }
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001362 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001363 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001364 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001365}
1366
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001367const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1368 const VarDecl *VD,
1369 const LocationContext *LC,
1370 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001371
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001372 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001373 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001374
Ted Kremenek0964a062009-01-21 06:57:53 +00001375 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001376 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001377 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001378 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001379
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001380 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001381}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001382
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001383// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001384const GRState *
1385RegionStoreManager::BindCompoundLiteral(const GRState *state,
1386 const CompoundLiteralExpr* CL,
1387 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001389 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001390 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001391}
1392
Ted Kremenek67f28532009-06-17 22:02:04 +00001393const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001394 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001395 SVal Init) {
1396
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001397 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001398 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001399 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001400
Ted Kremenek46537392009-07-16 01:33:37 +00001401 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001402
1403 // Check if the init expr is a StringLiteral.
1404 if (isa<loc::MemRegionVal>(Init)) {
1405 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1406 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1407 const char* str = S->getStrData();
1408 unsigned len = S->getByteLength();
1409 unsigned j = 0;
1410
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001411 // Copy bytes from the string literal into the target array. Trailing bytes
1412 // in the array that are not covered by the string literal are initialized
1413 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001414 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001415 if (j >= len)
1416 break;
1417
Ted Kremenek46537392009-07-16 01:33:37 +00001418 SVal Idx = ValMgr.makeArrayIndex(i);
1419 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1420 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001421
Zhongxing Xud91ee272009-06-23 09:02:15 +00001422 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001423 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001424 }
1425
Ted Kremenek67f28532009-06-17 22:02:04 +00001426 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001427 }
1428
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001429 // Handle lazy compound values.
1430 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1431 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001432
1433 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001434 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001435 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001436 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Ted Kremenek46537392009-07-16 01:33:37 +00001438 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001439 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001440 if (VI == VE)
1441 break;
1442
Ted Kremenek46537392009-07-16 01:33:37 +00001443 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001444 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001445
1446 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001447 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001448 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001449 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001450 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001451 }
1452
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001453 // If the init list is shorter than the array length, set the array default
1454 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001455 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001456 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001457 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001458 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001459 }
1460 }
1461
Ted Kremenek67f28532009-06-17 22:02:04 +00001462 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001463}
1464
Ted Kremenek67f28532009-06-17 22:02:04 +00001465const GRState *
1466RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1467 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001468
Ted Kremenek67f28532009-06-17 22:02:04 +00001469 if (!Features.supportsFields())
1470 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001471
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001472 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001473 assert(T->isStructureType());
1474
Ted Kremenek6217b802009-07-29 21:53:49 +00001475 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001476 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001477
1478 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001479 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001480
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001481 // Handle lazy compound values.
1482 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1483 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1486 // Ignore them and kill the field values.
1487 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1488 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001489
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001490 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001491 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001492
1493 RecordDecl::field_iterator FI, FE;
1494
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001495 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001496
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001497 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001498 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001499
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001500 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001501 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001502
Ted Kremenekcf549592009-09-22 21:19:14 +00001503 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001504 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001505 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001506 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001507 else
1508 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001509 }
1510
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001511 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001512 if (FI != FE)
1513 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001514
Ted Kremenek67f28532009-06-17 22:02:04 +00001515 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001516}
1517
Ted Kremenek67f28532009-06-17 22:02:04 +00001518const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001519 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001520
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001521 // Set the default value of the struct region to "unknown".
1522 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001523
1524 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001525 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001526 RegionBindings B = GetRegionBindings(store);
1527 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001528 const MemRegion* R = I.getKey();
1529 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1530 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001531 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001532 }
1533
Ted Kremenek67f28532009-06-17 22:02:04 +00001534 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001535}
1536
Ted Kremenek67f28532009-06-17 22:02:04 +00001537const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1538 const MemRegion* R, SVal V) {
1539 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001540}
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001542const GRState*
1543RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1544 const GRState *state,
1545 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001546
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001547 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001548 RegionBindings B = GetRegionBindings(state->getStore());
1549 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1550 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001551 state->get_context<RegionDefaultValue>();
1552
Mike Stump1eb44332009-09-09 15:08:12 +00001553 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001554 SubRegions(getRegionStoreSubRegionMap(state));
1555
Mike Stump1eb44332009-09-09 15:08:12 +00001556 // B and DVM are updated after the call to RemoveSubRegionBindings.
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001557 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001559 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1560 // results in a zero-copy algorithm.
1561 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1562}
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Ted Kremenek9af46f52009-06-16 22:36:44 +00001564//===----------------------------------------------------------------------===//
1565// State pruning.
1566//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Ted Kremenek9af46f52009-06-16 22:36:44 +00001568static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1569 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1570 const MemRegion *R = XR->getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Ted Kremenek9af46f52009-06-16 22:36:44 +00001572 while (R) {
1573 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1574 SymReaper.markLive(SR->getSymbol());
1575 return;
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Ted Kremenek9af46f52009-06-16 22:36:44 +00001578 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1579 R = SR->getSuperRegion();
1580 continue;
1581 }
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Ted Kremenek9af46f52009-06-16 22:36:44 +00001583 break;
1584 }
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenek9af46f52009-06-16 22:36:44 +00001586 return;
1587 }
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Ted Kremenek9af46f52009-06-16 22:36:44 +00001589 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1590 SymReaper.markLive(*SI);
1591}
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Ted Kremenek451ac092009-08-06 04:50:20 +00001593namespace {
1594class VISIBILITY_HIDDEN TreeScanner {
1595 RegionBindings B;
1596 RegionDefaultBindings DB;
1597 SymbolReaper &SymReaper;
1598 llvm::DenseSet<const MemRegion*> &Marked;
1599 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1600 RegionStoreSubRegionMap &M;
1601 RegionStoreManager &RS;
1602 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1603 const bool MarkKeys;
1604public:
1605 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1606 SymbolReaper &symReaper,
1607 llvm::DenseSet<const MemRegion*> &marked,
1608 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1609 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1610 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1611 bool markKeys = true)
1612 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1613 ScannedLazyVals(scannedLazyVals), M(m),
1614 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Ted Kremenek451ac092009-08-06 04:50:20 +00001616 void scanTree(const MemRegion *R);
1617};
1618} // end anonymous namespace
Mike Stump1eb44332009-09-09 15:08:12 +00001619
1620
Ted Kremenek451ac092009-08-06 04:50:20 +00001621void TreeScanner::scanTree(const MemRegion *R) {
1622 if (MarkKeys) {
1623 if (Marked.count(R))
Mike Stump1eb44332009-09-09 15:08:12 +00001624 return;
1625
Ted Kremenek451ac092009-08-06 04:50:20 +00001626 Marked.insert(R);
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremenek451ac092009-08-06 04:50:20 +00001629 // Mark the symbol for any live SymbolicRegion as "live". This means we
1630 // should continue to track that symbol.
1631 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1632 SymReaper.markLive(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Ted Kremenek451ac092009-08-06 04:50:20 +00001634 // Get the data binding for R (if any).
1635 const SVal* Xptr = B.lookup(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenek451ac092009-08-06 04:50:20 +00001637 // Check for lazy bindings.
1638 if (const nonloc::LazyCompoundVal *V =
1639 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001640
1641 const LazyCompoundValData *D = V->getCVData();
Ted Kremenek451ac092009-08-06 04:50:20 +00001642
1643 if (!ScannedLazyVals.count(D)) {
1644 // Scan the bindings in the LazyCompoundVal.
1645 ScannedLazyVals.insert(D);
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Ted Kremenek451ac092009-08-06 04:50:20 +00001647 // FIXME: Cache subregion maps.
1648 const GRState *lazyState = D->getState();
1649
1650 llvm::OwningPtr<RegionStoreSubRegionMap>
1651 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenek451ac092009-08-06 04:50:20 +00001653 Store lazyStore = lazyState->getStore();
1654 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Ted Kremenek451ac092009-08-06 04:50:20 +00001656 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek451ac092009-08-06 04:50:20 +00001658 // Scan the bindings.
1659 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1660 *lazySM.get(), RS, RegionRoots, false);
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Ted Kremenek451ac092009-08-06 04:50:20 +00001662 scan.scanTree(D->getRegion());
1663 }
1664 }
Mike Stump1eb44332009-09-09 15:08:12 +00001665 else {
1666 // No direct binding? Get the default binding for R (if any).
Ted Kremenek451ac092009-08-06 04:50:20 +00001667 if (!Xptr)
1668 Xptr = DB.lookup(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Ted Kremenek451ac092009-08-06 04:50:20 +00001670 // Direct or default binding?
1671 if (Xptr) {
1672 SVal X = *Xptr;
1673 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Ted Kremenek451ac092009-08-06 04:50:20 +00001675 // If X is a region, then add it to the RegionRoots.
1676 if (const MemRegion *RX = X.getAsRegion()) {
1677 RegionRoots.push_back(RX);
1678 // Mark the super region of the RX as live.
Mike Stump1eb44332009-09-09 15:08:12 +00001679 // e.g.: int x; char *y = (char*) &x; if (*y) ...
Ted Kremenek451ac092009-08-06 04:50:20 +00001680 // 'y' => element region. 'x' is its super region.
1681 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1682 RegionRoots.push_back(SR->getSuperRegion());
1683 }
1684 }
1685 }
1686 }
Mike Stump1eb44332009-09-09 15:08:12 +00001687
1688 RegionStoreSubRegionMap::iterator I, E;
Ted Kremenek451ac092009-08-06 04:50:20 +00001689
1690 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1691 scanTree(*I);
1692}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001693
Mike Stump1eb44332009-09-09 15:08:12 +00001694void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001695 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001696 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001697{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001698 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001699 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Ted Kremenek9af46f52009-06-16 22:36:44 +00001701 // Lazily constructed backmap from MemRegions to SubRegions.
1702 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1703 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Ted Kremenek9af46f52009-06-16 22:36:44 +00001705 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001706 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001707 SubRegions(getRegionStoreSubRegionMap(&state));
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Ted Kremenek9af46f52009-06-16 22:36:44 +00001709 // Do a pass over the regions in the store. For VarRegions we check if
1710 // the variable is still live and if so add it to the list of live roots.
Mike Stump1eb44332009-09-09 15:08:12 +00001711 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001712 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001714 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001715 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001716 const MemRegion *R = I.getKey();
1717 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001718 }
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001720 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001721 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1722 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001723 I != E; ++I) {
1724 const MemRegion *R = I.getKey();
1725 IntermediateRoots.push_back(R);
1726 }
1727
1728 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001729 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001730 while (!IntermediateRoots.empty()) {
1731 const MemRegion* R = IntermediateRoots.back();
1732 IntermediateRoots.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Ted Kremenek9af46f52009-06-16 22:36:44 +00001734 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001735 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001736 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001737 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001738 continue;
1739 }
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001741 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001742 if (SymReaper.isLive(SR->getSymbol()))
1743 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001744 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001745 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001746
1747 // Add the super region for R to the worklist if it is a subregion.
1748 if (const SubRegion* superR =
1749 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1750 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001751 }
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Ted Kremenek9af46f52009-06-16 22:36:44 +00001753 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
Mike Stump1eb44332009-09-09 15:08:12 +00001754 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001755 llvm::DenseSet<const MemRegion*> Marked;
1756 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1757 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1758 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001759
Ted Kremenek451ac092009-08-06 04:50:20 +00001760 while (!RegionRoots.empty()) {
1761 const MemRegion *R = RegionRoots.back();
1762 RegionRoots.pop_back();
1763 TS.scanTree(R);
Mike Stump1eb44332009-09-09 15:08:12 +00001764 }
1765
Ted Kremenek9af46f52009-06-16 22:36:44 +00001766 // We have now scanned the store, marking reachable regions and symbols
1767 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001768 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001769 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001770 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001771 // If this region live? Is so, none of its symbols are dead.
1772 if (Marked.count(R))
1773 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Ted Kremenek9af46f52009-06-16 22:36:44 +00001775 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001776 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Ted Kremenek9af46f52009-06-16 22:36:44 +00001778 // Mark all non-live symbols that this region references as dead.
1779 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1780 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Ted Kremenek9af46f52009-06-16 22:36:44 +00001782 SVal X = I.getData();
1783 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001784 for (; SI != SE; ++SI)
1785 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001786 }
Mike Stump1eb44332009-09-09 15:08:12 +00001787
1788 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001789 RegionDefaultBindings NewDVM = DVM;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001791 state.get_context<RegionDefaultValue>();
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Ted Kremenek451ac092009-08-06 04:50:20 +00001793 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001794 I != E; ++I) {
1795 const MemRegion *R = I.getKey();
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Ted Kremenek093569c2009-08-02 05:00:15 +00001797 // If this region live? Is so, none of its symbols are dead.
1798 if (Marked.count(R))
1799 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Ted Kremenek093569c2009-08-02 05:00:15 +00001801 // Remove this dead region.
1802 NewDVM = DVMFactory.Remove(NewDVM, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek093569c2009-08-02 05:00:15 +00001804 // Mark all non-live symbols that this region references as dead.
1805 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1806 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Ted Kremenek093569c2009-08-02 05:00:15 +00001808 SVal X = I.getData();
1809 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1810 for (; SI != SE; ++SI)
1811 SymReaper.maybeDead(*SI);
1812 }
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001814 // Write the store back.
1815 state.setStore(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Ted Kremenek093569c2009-08-02 05:00:15 +00001817 // Write the updated default bindings back.
1818 // FIXME: Right now this involves a fetching of a persistent state.
1819 // We can do better.
1820 if (DVM != NewDVM)
1821 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001822}
1823
1824//===----------------------------------------------------------------------===//
1825// Utility methods.
1826//===----------------------------------------------------------------------===//
1827
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001828void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001829 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001830 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001831 OS << "Store (direct bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Ted Kremenek451ac092009-08-06 04:50:20 +00001833 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001834 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001835}