blob: 84c8195ecb139171a6fb0d5136790478dfd27dea [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"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000021#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000022
23#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000024#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000025#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000026#include "llvm/Support/Compiler.h"
27
28using namespace clang;
29
Zhongxing Xubaf03a72008-11-24 09:44:56 +000030// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000031typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000032
Ted Kremenek50dc1b32008-12-24 01:05:03 +000033//===----------------------------------------------------------------------===//
34// Region "Views"
35//===----------------------------------------------------------------------===//
36//
37// MemRegions can be layered on top of each other. This GDM entry tracks
38// what are the MemRegions that layer a given MemRegion.
39//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000040typedef llvm::ImmutableSet<const MemRegion*> RegionViews;
Ted Kremenek50dc1b32008-12-24 01:05:03 +000041namespace { class VISIBILITY_HIDDEN RegionViewMap {}; }
42static int RegionViewMapIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000043namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000044 template<> struct GRStateTrait<RegionViewMap>
45 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
46 RegionViews> > {
47
48 static void* GDMIndex() { return &RegionViewMapIndex; }
49 };
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000050}
Zhongxing Xu17892752008-10-08 02:50:44 +000051
Zhongxing Xu20794942009-05-06 08:33:50 +000052// RegionCasts records the current cast type of a region.
53namespace { class VISIBILITY_HIDDEN RegionCasts {}; }
54static int RegionCastsIndex = 0;
55namespace clang {
56 template<> struct GRStateTrait<RegionCasts>
57 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*,
58 QualType> > {
59 static void* GDMIndex() { return &RegionCastsIndex; }
60 };
61}
62
Ted Kremenek50dc1b32008-12-24 01:05:03 +000063//===----------------------------------------------------------------------===//
64// Region "Extents"
65//===----------------------------------------------------------------------===//
66//
67// MemRegions represent chunks of memory with a size (their "extent"). This
68// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000069//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000070namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
71static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000072namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000073 template<> struct GRStateTrait<RegionExtents>
74 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
75 static void* GDMIndex() { return &RegionExtentsIndex; }
76 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000077}
78
Ted Kremenek50dc1b32008-12-24 01:05:03 +000079//===----------------------------------------------------------------------===//
80// Region "killsets".
81//===----------------------------------------------------------------------===//
82//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000083// RegionStore lazily adds value bindings to regions when the analyzer handles
84// assignment statements. Killsets track which default values have been
85// killed, thus distinguishing between "unknown" values and default
86// values. Regions are added to killset only when they are assigned "unknown"
87// directly, otherwise we should have their value in the region bindings.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000088//
89namespace { class VISIBILITY_HIDDEN RegionKills {}; }
Ted Kremenek2ed14be2008-12-05 00:47:52 +000090static int RegionKillsIndex = 0;
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000091namespace clang {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000092 template<> struct GRStateTrait<RegionKills>
Ted Kremenek50dc1b32008-12-24 01:05:03 +000093 : public GRStatePartialTrait< llvm::ImmutableSet<const MemRegion*> > {
Ted Kremenek2ed14be2008-12-05 00:47:52 +000094 static void* GDMIndex() { return &RegionKillsIndex; }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +000095 };
96}
97
Ted Kremenek50dc1b32008-12-24 01:05:03 +000098//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000099// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000100//===----------------------------------------------------------------------===//
101//
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000102// This GDM entry tracks what regions have a default value if they have no bound
103// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000104//
105namespace { class VISIBILITY_HIDDEN RegionDefaultValue {}; }
106static int RegionDefaultValueIndex = 0;
107namespace clang {
108 template<> struct GRStateTrait<RegionDefaultValue>
109 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
110 static void* GDMIndex() { return &RegionDefaultValueIndex; }
111 };
112}
113
114//===----------------------------------------------------------------------===//
115// Main RegionStore logic.
116//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000117
Zhongxing Xu17892752008-10-08 02:50:44 +0000118namespace {
119
Ted Kremenek59e8f112009-03-03 01:35:36 +0000120class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
121 typedef llvm::DenseMap<const MemRegion*,
122 llvm::ImmutableSet<const MemRegion*> > Map;
123
124 llvm::ImmutableSet<const MemRegion*>::Factory F;
125 Map M;
126
127public:
128 void add(const MemRegion* Parent, const MemRegion* SubRegion) {
129 Map::iterator I = M.find(Parent);
130 M.insert(std::make_pair(Parent,
131 F.Add(I == M.end() ? F.GetEmptySet() : I->second, SubRegion)));
132 }
133
134 ~RegionStoreSubRegionMap() {}
135
Ted Kremenek5dc27462009-03-03 02:51:43 +0000136 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000137 Map::iterator I = M.find(Parent);
138
139 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000140 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000141
142 llvm::ImmutableSet<const MemRegion*> S = I->second;
143 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
144 SI != SE; ++SI) {
145 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000146 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000148
149 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000150 }
151};
152
Zhongxing Xu17892752008-10-08 02:50:44 +0000153class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
154 RegionBindingsTy::Factory RBFactory;
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000155 RegionViews::Factory RVFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000156
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000157 const MemRegion* SelfRegion;
158 const ImplicitParamDecl *SelfDecl;
Zhongxing Xu17892752008-10-08 02:50:44 +0000159
160public:
161 RegionStoreManager(GRStateManager& mgr)
Ted Kremenekc62abc12009-04-21 21:51:34 +0000162 : StoreManager(mgr),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000163 RBFactory(mgr.getAllocator()),
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000164 RVFactory(mgr.getAllocator()),
Ted Kremenekc62abc12009-04-21 21:51:34 +0000165 SelfRegion(0), SelfDecl(0) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000166 if (const ObjCMethodDecl* MD =
167 dyn_cast<ObjCMethodDecl>(&StateMgr.getCodeDecl()))
168 SelfDecl = MD->getSelfDecl();
169 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000170
171 virtual ~RegionStoreManager() {}
172
Ted Kremenek14453bf2009-03-03 19:02:42 +0000173 SubRegionMap* getSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000174
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000175 const GRState* BindCompoundLiteral(const GRState* St,
176 const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000177
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000178 /// getLValueString - Returns an SVal representing the lvalue of a
179 /// StringLiteral. Within RegionStore a StringLiteral has an
180 /// associated StringRegion, and the lvalue of a StringLiteral is
181 /// the lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000182 SVal getLValueString(const GRState* St, const StringLiteral* S);
183
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000184 /// getLValueCompoundLiteral - Returns an SVal representing the
185 /// lvalue of a compound literal. Within RegionStore a compound
186 /// literal has an associated region, and the lvalue of the
187 /// compound literal is the lvalue of that region.
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000188 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
189
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000190 /// getLValueVar - Returns an SVal that represents the lvalue of a
191 /// variable. Within RegionStore a variable has an associated
192 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000193 SVal getLValueVar(const GRState* St, const VarDecl* VD);
194
195 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
196
197 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000198
199 SVal getLValueFieldOrIvar(const GRState* St, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000200
Ted Kremenekf936f452009-05-04 06:18:28 +0000201 SVal getLValueElement(const GRState* St, QualType elementType,
202 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000203
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000204 SVal getSizeInElements(const GRState* St, const MemRegion* R);
205
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000206 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
207 /// type. 'Array' represents the lvalue of the array being decayed
208 /// to a pointer, and the returned SVal represents the decayed
209 /// version of that lvalue (i.e., a pointer to the first element of
210 /// the array). This is called by GRExprEngine when evaluating
211 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000212 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000213
Zhongxing Xu88980592009-05-06 02:42:32 +0000214 CastResult CastRegion(const GRState* state, const MemRegion* R,
215 QualType CastToTy);
216
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000217 SVal EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R);
218
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000219 /// The high level logic for this method is this:
220 /// Retrieve (L)
221 /// if L has binding
222 /// return L's binding
223 /// else if L is in killset
224 /// return unknown
225 /// else
226 /// if L is on stack or heap
227 /// return undefined
228 /// else
229 /// return symbolic
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000230 SVal Retrieve(const GRState* state, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000231
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000232 const GRState* Bind(const GRState* St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000233
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000234 Store Remove(Store store, Loc LV);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000235
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000236 Store getInitialStore() { return RBFactory.GetEmptyMap().getRoot(); }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000237
238 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
239 /// 'this' object (C++). When used when analyzing a normal function this
240 /// method returns NULL.
241 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000242 if (!SelfDecl)
243 return 0;
244
245 if (!SelfRegion) {
246 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(&StateMgr.getCodeDecl());
247 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
248 MRMgr.getHeapRegion());
249 }
250
251 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000252 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000253
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000254 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
255 /// It returns a new Store with these values removed, and populates LSymbols
256 // and DSymbols with the known set of live and dead symbols respectively.
257 Store RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000258 SymbolReaper& SymReaper,
259 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000260
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000261 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal);
262
263 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
264 return St;
265 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000266
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000267 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
Zhongxing Xu20794942009-05-06 08:33:50 +0000268 const GRState* setCastType(const GRState* St, const MemRegion* R, QualType T);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000269
Zhongxing Xu17892752008-10-08 02:50:44 +0000270 static inline RegionBindingsTy GetRegionBindings(Store store) {
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000271 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000272 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000273
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000274 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000275
276 void iterBindings(Store store, BindingsHandler& f) {
277 // FIXME: Implement.
278 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000279
280private:
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000281 const GRState* BindArray(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000282
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000283 /// Retrieve the values in a struct and return a CompoundVal, used when doing
284 /// struct copy:
285 /// struct s x, y;
286 /// x = y;
287 /// y's value is retrieved by this method.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000288 SVal RetrieveStruct(const GRState* St, const TypedRegion* R);
Zhongxing Xu0b242ec2008-12-04 01:12:41 +0000289
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000290 SVal RetrieveArray(const GRState* St, const TypedRegion* R);
291
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000292 const GRState* BindStruct(const GRState* St, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000293
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000294 /// KillStruct - Set the entire struct to unknown.
295 const GRState* KillStruct(const GRState* St, const TypedRegion* R);
296
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000297 // Utility methods.
298 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
299 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xua15f7ac2009-05-08 01:33:18 +0000300
Zhongxing Xu63123d82008-11-23 04:30:35 +0000301 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000302
303 const GRState* AddRegionView(const GRState* St,
304 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +0000305 const GRState* RemoveRegionView(const GRState* St,
306 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000307};
308
309} // end anonymous namespace
310
Ted Kremenek95c7b002008-10-24 01:04:59 +0000311StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000312 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000313}
314
Ted Kremenek14453bf2009-03-03 19:02:42 +0000315SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000316 RegionBindingsTy B = GetRegionBindings(state->getStore());
317 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
318
319 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
320 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
321 M->add(R->getSuperRegion(), R);
322 }
323
Ted Kremenek14453bf2009-03-03 19:02:42 +0000324 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000325}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000326
327/// getLValueString - Returns an SVal representing the lvalue of a
328/// StringLiteral. Within RegionStore a StringLiteral has an
329/// associated StringRegion, and the lvalue of a StringLiteral is the
330/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000331SVal RegionStoreManager::getLValueString(const GRState* St,
332 const StringLiteral* S) {
333 return loc::MemRegionVal(MRMgr.getStringRegion(S));
334}
335
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000336/// getLValueVar - Returns an SVal that represents the lvalue of a
337/// variable. Within RegionStore a variable has an associated
338/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000339SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
340 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
341}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000342
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000343/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
344/// of a compound literal. Within RegionStore a compound literal
345/// has an associated region, and the lvalue of the compound literal
346/// is the lvalue of that region.
347SVal
348RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
349 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000350 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
351}
352
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000353SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
354 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000355 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000356}
357
358SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
359 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000360 return getLValueFieldOrIvar(St, Base, D);
361}
362
363SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
364 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000365 if (Base.isUnknownOrUndef())
366 return Base;
367
368 Loc BaseL = cast<Loc>(Base);
369 const MemRegion* BaseR = 0;
370
371 switch (BaseL.getSubKind()) {
372 case loc::MemRegionKind:
373 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
374 break;
375
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000376 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000377 // These are anormal cases. Flag an undefined value.
378 return UndefinedVal();
379
380 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000381 // While these seem funny, this can happen through casts.
382 // FIXME: What we should return is the field offset. For example,
383 // add the field offset to the integer value. That way funny things
384 // like this work properly: &(((struct foo *) 0xa)->f)
385 return Base;
386
387 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000388 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000389 return Base;
390 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000391
392 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
393 // of FieldDecl.
394 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
395 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000396
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000397 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000398}
399
Ted Kremenekf936f452009-05-04 06:18:28 +0000400SVal RegionStoreManager::getLValueElement(const GRState* St,
401 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000402 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000403
Ted Kremenekde7ec632009-03-09 22:44:49 +0000404 // If the base is an unknown or undefined value, just return it back.
405 // FIXME: For absolute pointer addresses, we just return that value back as
406 // well, although in reality we should return the offset added to that
407 // value.
408 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000409 return Base;
410
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000411 // Only handle integer offsets... for now.
412 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000413 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000414
Zhongxing Xuce760782009-05-09 13:20:07 +0000415 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000416
417 // Pointer of any type can be cast and used as array base.
418 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
419
420 if (!ElemR) {
421 //
422 // If the base region is not an ElementRegion, create one.
423 // This can happen in the following example:
424 //
425 // char *p = __builtin_alloc(10);
426 // p[1] = 8;
427 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000428 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000429 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000430
431 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
432 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
433 const llvm::APSInt& OffI = CI->getValue();
434 if (OffI.isUnsigned()) {
435 llvm::APSInt Tmp = OffI;
436 Tmp.setIsSigned(true);
437 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
438 }
439 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000440 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
441 BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000442 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000443
444 SVal BaseIdx = ElemR->getIndex();
445
446 if (!isa<nonloc::ConcreteInt>(BaseIdx))
447 return UnknownVal();
448
449 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
450 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
451 assert(BaseIdxI.isSigned());
452
453 // FIXME: This appears to be the assumption of this code. We should review
454 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
455 // can't we need to put a comment here. If it can, we should handle it.
456 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000457
Zhongxing Xuce760782009-05-09 13:20:07 +0000458 const MemRegion *ArrayR = ElemR->getSuperRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000459 SVal NewIdx;
460
461 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
462 // 'Offset' might be unsigned. We have to convert it to signed and
463 // possibly extend it.
464 llvm::APSInt Tmp = OffI;
465
466 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
467 Tmp.extend(BaseIdxI.getBitWidth());
468
469 Tmp.setIsSigned(true);
470 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000471 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000472 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000473 else
474 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000475
Ted Kremenekf936f452009-05-04 06:18:28 +0000476 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000477}
478
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000479SVal RegionStoreManager::getSizeInElements(const GRState* St,
480 const MemRegion* R) {
481 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
482 // Get the type of the variable.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000483 QualType T = VR->getDesugaredValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000484
Ted Kremenek14553ab2009-01-30 00:08:43 +0000485 // FIXME: Handle variable-length arrays.
486 if (isa<VariableArrayType>(T))
487 return UnknownVal();
488
489 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
490 // return the size as signed integer.
491 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
492 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000493
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000494 GRStateRef state(St, StateMgr);
495 const QualType* CastTy = state.get<RegionCasts>(VR);
496
Zhongxing Xue6536af2009-05-08 02:00:55 +0000497 // If the VarRegion is cast to other type, compute the size with respect to
498 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000499 if (CastTy) {
500 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000501 QualType VarTy = VR->getValueType(getContext());
Zhongxing Xue6536af2009-05-08 02:00:55 +0000502 uint64_t EleSize = getContext().getTypeSize(EleTy);
503 uint64_t VarSize = getContext().getTypeSize(VarTy);
504 return NonLoc::MakeIntVal(getBasicVals(), VarSize / EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000505 }
506
Ted Kremenek14553ab2009-01-30 00:08:43 +0000507 // Clients can use ordinary variables as if they were arrays. These
508 // essentially are arrays of size 1.
509 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000510 }
511
512 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000513 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000514 // We intentionally made the size value signed because it participates in
515 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000516 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000517 }
518
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000519 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
520 // FIXME: Unsupported yet.
521 FR = 0;
522 return UnknownVal();
523 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000524
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000525 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000526 return UnknownVal();
527 }
528
Zhongxing Xuce760782009-05-09 13:20:07 +0000529 if (isa<AllocaRegion>(R)) {
530 return UnknownVal();
531 }
532
Zhongxing Xuccb16162009-05-06 08:08:27 +0000533 if (isa<ElementRegion>(R)) {
534 return UnknownVal();
535 }
536
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000537 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000538 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000539}
540
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000541/// ArrayToPointer - Emulates the "decay" of an array to a pointer
542/// type. 'Array' represents the lvalue of the array being decayed
543/// to a pointer, and the returned SVal represents the decayed
544/// version of that lvalue (i.e., a pointer to the first element of
545/// the array). This is called by GRExprEngine when evaluating casts
546/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000547SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000548 if (!isa<loc::MemRegionVal>(Array))
549 return UnknownVal();
550
551 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
552 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
553
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000554 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000555 return UnknownVal();
556
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000557 // Strip off typedefs from the ArrayRegion's ValueType.
558 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000559 ArrayType *AT = cast<ArrayType>(T);
560 T = AT->getElementType();
561
Zhongxing Xu63123d82008-11-23 04:30:35 +0000562 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000563 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR);
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000564
565 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000566}
567
Zhongxing Xu88980592009-05-06 02:42:32 +0000568RegionStoreManager::CastResult
569RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000570 QualType CastToTy) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000571
572 ASTContext& Ctx = StateMgr.getContext();
573
574 // We need to know the real type of CastToTy.
575 QualType ToTy = Ctx.getCanonicalType(CastToTy);
576
577 // Check cast to ObjCQualifiedID type.
578 if (isa<ObjCQualifiedIdType>(ToTy)) {
579 // FIXME: Record the type information aside.
580 return CastResult(state, R);
581 }
582
583 // CodeTextRegion should be cast to only function pointer type.
584 if (isa<CodeTextRegion>(R)) {
585 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType());
586 return CastResult(state, R);
587 }
588
Zhongxing Xudb3a0982009-05-09 10:03:08 +0000589 // Now assume we are casting from pointer to pointer. Other cases should
590 // already be handled.
Zhongxing Xu88980592009-05-06 02:42:32 +0000591 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
592
Zhongxing Xu88980592009-05-06 02:42:32 +0000593 // Process region cast according to the kind of the region being cast.
594
Zhongxing Xu88980592009-05-06 02:42:32 +0000595 // FIXME: Need to handle arbitrary downcasts.
Zhongxing Xu88980592009-05-06 02:42:32 +0000596 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
Zhongxing Xuce760782009-05-09 13:20:07 +0000597 state = setCastType(state, R, ToTy);
598 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000599 }
600
601 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
602 // they should not be cast. We only layer an ElementRegion when the cast-to
603 // pointee type is of smaller size. In other cases, we return the original
604 // VarRegion.
605 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
606 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xu2572eda2009-05-08 07:28:25 +0000607 // If the pointee type is incomplete, do not compute its size, and return
608 // the original region.
609 if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) {
610 const RecordDecl *D = RT->getDecl();
611 if (!D->getDefinition(getContext()))
612 return CastResult(state, R);
613 }
614
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000615 QualType ObjTy = cast<TypedRegion>(R)->getValueType(getContext());
Zhongxing Xufb1e3312009-05-08 02:12:59 +0000616 uint64_t PointeeTySize = getContext().getTypeSize(PointeeTy);
617 uint64_t ObjTySize = getContext().getTypeSize(ObjTy);
618
Zhongxing Xu5bf32872009-05-09 15:34:29 +0000619 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
620 (ObjTy->isAggregateType() && PointeeTy->isScalarType())) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000621 // Record the cast type of the region.
622 state = setCastType(state, R, ToTy);
623
Zhongxing Xuccb16162009-05-06 08:08:27 +0000624 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu20794942009-05-06 08:33:50 +0000625 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx, R);
Zhongxing Xuccb16162009-05-06 08:08:27 +0000626 return CastResult(state, ER);
627 } else
628 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000629 }
630
Zhongxing Xu88980592009-05-06 02:42:32 +0000631 if (isa<ObjCObjectRegion>(R)) {
632 return CastResult(state, R);
633 }
634
635 assert(0 && "Unprocessed region.");
636}
637
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000638SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000639 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000640 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000641 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000642
Zhongxing Xua1718c72009-04-03 07:33:13 +0000643 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000644 const ElementRegion *ER = 0;
645 // If the operand is a symbolic region, we convert it to the first element
646 // region implicitly.
647 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) {
648 // Get symbol's type. It should be a pointer type.
649 SymbolRef Sym = SR->getSymbol();
650 QualType T = Sym->getType(getContext());
651 QualType EleTy = cast<PointerType>(T.getTypePtr())->getPointeeType();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000652
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000653 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
654 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR);
655 } else
656 ER = cast<ElementRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000657
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000658 SVal Idx = ER->getIndex();
659
660 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
661 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
662
663 // Only support concrete integer indexes for now.
664 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000665 // FIXME: For now, convert the signedness and bitwidth of offset in case
666 // they don't match. This can result from pointer arithmetic. In reality,
667 // we should figure out what are the proper semantics and implement them.
668 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000669 // This addresses the test case test/Analysis/ptr-arith.c
670 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000671 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
672 Offset->getValue()));
673 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000674 const MemRegion* NewER =
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000675 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion());
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000676 return Loc::MakeVal(NewER);
677
Ted Kremenek5dc27462009-03-03 02:51:43 +0000678 }
679
680 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000681}
682
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000683SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000684 assert(!isa<UnknownVal>(L) && "location unknown");
685 assert(!isa<UndefinedVal>(L) && "location undefined");
686
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000687 // FIXME: Is this even possible? Shouldn't this be treated as a null
688 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000689 if (isa<loc::ConcreteInt>(L))
690 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000691
Zhongxing Xua1718c72009-04-03 07:33:13 +0000692 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
693
694 // We return unknown for symbolic region for now. This might be improved.
695 // Example:
696 // void f(int* p) { int x = *p; }
697 if (isa<SymbolicRegion>(MR))
698 return UnknownVal();
699
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000700 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
701 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000702 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000703 assert(R && "bad region");
704
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000705 // FIXME: We should eventually handle funny addressing. e.g.:
706 //
707 // int x = ...;
708 // int *p = &x;
709 // char *q = (char*) p;
710 // char c = *q; // returns the first byte of 'x'.
711 //
712 // Such funny addressing will occur due to layering of regions.
713
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000714 QualType RTy = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000715
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000716 if (RTy->isStructureType())
717 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000718
719 if (RTy->isArrayType())
720 return RetrieveArray(St, R);
721
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000722 // FIXME: handle Vector types.
723 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000724 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000725
726 RegionBindingsTy B = GetRegionBindings(St->getStore());
727 RegionBindingsTy::data_type* V = B.lookup(R);
728
729 // Check if the region has a binding.
730 if (V)
731 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000732
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000733 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000734
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000735 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000736 if (state.contains<RegionKills>(R))
737 return UnknownVal();
738
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000739 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000740 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
741 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
742 GRStateTrait<RegionDefaultValue>::lookup_type D =
743 state.get<RegionDefaultValue>(SuperR);
744 if (D)
745 return *D;
746 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000747
748 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
749 const MemRegion *SR = IVR->getSuperRegion();
750
751 // If the super region is 'self' then return the symbol representing
752 // the value of the ivar upon entry to the method.
753 if (SR == SelfRegion) {
754 // FIXME: Do we need to handle the case where the super region
755 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000756 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000757 }
758
759 // Otherwise, we need a new symbol. For now return Unknown.
760 return UnknownVal();
761 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000762
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000763 // The location does not have a bound value. This means that it has
764 // the value it had upon its creation and/or entry to the analyzed
765 // function/method. These are either symbolic values or 'undefined'.
766
767 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000768 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
769 const VarDecl *VD = VR->getDecl();
770
Ted Kremenekcec35252009-03-04 00:23:05 +0000771 if (VD == SelfDecl)
772 return loc::MemRegionVal(getSelfRegion(0));
773
774 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
775 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000776 QualType VTy = VD->getType();
777 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000778 return ValMgr.getRegionValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000779 else
780 return UnknownVal();
781 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000782 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000783
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000784 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
785 // All stack variables are considered to have undefined values
786 // upon creation. All heap allocated blocks are considered to
787 // have undefined values as well unless they are explicitly bound
788 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000789 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000790 }
791
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000792 // All other integer values are symbolic.
793 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000794 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000795 else
796 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000797}
798
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000799SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000800 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000801 assert(T->isStructureType());
802
803 const RecordType* RT = cast<RecordType>(T.getTypePtr());
804 RecordDecl* RD = RT->getDecl();
805 assert(RD->isDefinition());
806
807 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
808
Douglas Gregor6ab35242009-04-09 21:40:53 +0000809 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
810 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000811
Douglas Gregore267ff32008-12-11 20:41:00 +0000812 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
813 FieldEnd = Fields.rend();
814 Field != FieldEnd; ++Field) {
815 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000816 QualType FTy = (*Field)->getType();
817 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000818 StructVal = getBasicVals().consVals(FieldValue, StructVal);
819 }
820
821 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
822}
823
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000824SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000825 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000826 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
827
828 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000829 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +0000830 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000831
832 for (; i < Size; ++i) {
833 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu2ee52142009-05-11 12:48:56 +0000834 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R);
Ted Kremenekf936f452009-05-04 06:18:28 +0000835 QualType ETy = ER->getElementType();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000836 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
837 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
838 }
839
840 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
841}
842
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000843const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000844 // If we get here, the location should be a region.
845 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000846 assert(R);
847
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000848 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000849 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000850 if (TR->getValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000851 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000852
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000853 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000854 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000855
856 if (V.isUnknown()) {
857 // Remove the binding.
858 store = RBFactory.Remove(B, R).getRoot();
859
860 // Add the region to the killset.
861 GRStateRef state(St, StateMgr);
862 St = state.add<RegionKills>(R);
863 }
864 else
865 store = RBFactory.Add(B, R, V).getRoot();
866
867 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000868}
869
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000870Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000871 const MemRegion* R = 0;
872
873 if (isa<loc::MemRegionVal>(L))
874 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000875
876 if (R) {
877 RegionBindingsTy B = GetRegionBindings(store);
878 return RBFactory.Remove(B, R).getRoot();
879 }
880
881 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000882}
883
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000884const GRState* RegionStoreManager::BindDecl(const GRState* St,
885 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000886
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000887 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000888 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000889
Ted Kremenek0964a062009-01-21 06:57:53 +0000890 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000891 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000892 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000893 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000894
Ted Kremenek0964a062009-01-21 06:57:53 +0000895 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000896}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000897
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000898// FIXME: this method should be merged into Bind().
899const GRState*
900RegionStoreManager::BindCompoundLiteral(const GRState* St,
901 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000902 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000903 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000904}
905
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000906const GRState* RegionStoreManager::setExtent(const GRState* St,
907 const MemRegion* R, SVal Extent) {
908 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000909 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000910}
911
912
Ted Kremenek241677a2009-01-21 22:26:05 +0000913static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000914 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
915 const MemRegion *R = XR->getRegion();
916
917 while (R) {
918 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
919 SymReaper.markLive(SR->getSymbol());
920 return;
921 }
922
923 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
924 R = SR->getSuperRegion();
925 continue;
926 }
927
928 break;
929 }
930
931 return;
932 }
933
Ted Kremenek241677a2009-01-21 22:26:05 +0000934 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
935 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000936}
937
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000938Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000939 SymbolReaper& SymReaper,
940 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
941{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000942
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000943 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000944 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000945
946 // Lazily constructed backmap from MemRegions to SubRegions.
947 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
948 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
949
950 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
951 // the ability to reuse memory. This way we can keep TmpAlloc around as
952 // an instance variable of RegionStoreManager (avoiding repeated malloc
953 // overhead).
954 llvm::BumpPtrAllocator TmpAlloc;
955
956 // Factory objects.
957 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
958 SubRegionsTy::Factory SubRegF(TmpAlloc);
959
960 // The backmap from regions to subregions.
961 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
962
963 // Do a pass over the regions in the store. For VarRegions we check if
964 // the variable is still live and if so add it to the list of live roots.
965 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000966
967 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
968
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000969 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000970 IntermediateRoots.push_back(I.getKey());
971 }
972
973 while (!IntermediateRoots.empty()) {
974 const MemRegion* R = IntermediateRoots.back();
975 IntermediateRoots.pop_back();
976
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000977 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000978 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000979 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +0000980 }
981 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
982 if (SymReaper.isLive(SR->getSymbol()))
983 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000984 }
985 else {
986 // Get the super region for R.
987 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000988
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000989 // Get the current set of subregions for SuperR.
990 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
Zhongxing Xua3eda832009-05-08 23:28:07 +0000991 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000992
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000993 // Add R to the subregions of SuperR.
Zhongxing Xua3eda832009-05-08 23:28:07 +0000994 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SRs, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +0000995
996 // Super region may be VarRegion or subregion of another VarRegion. Add it
997 // to the work list.
998 if (isa<SubRegion>(SuperR))
999 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001000 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001001 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001002
1003 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1004 // of the store. We want to find all live symbols and dead regions.
1005 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1006
1007 while (!RegionRoots.empty()) {
1008 // Dequeue the next region on the worklist.
1009 const MemRegion* R = RegionRoots.back();
1010 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001011
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001012 // Check if we have already processed this region.
1013 if (Marked.count(R)) continue;
1014
1015 // Mark this region as processed. This is needed for termination in case
1016 // a region is referenced more than once.
1017 Marked.insert(R);
1018
1019 // Mark the symbol for any live SymbolicRegion as "live". This means we
1020 // should continue to track that symbol.
1021 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001022 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001023
1024 // Get the data binding for R (if any).
1025 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1026 if (Xptr) {
1027 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001028 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001029
1030 // If X is a region, then add it the RegionRoots.
1031 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1032 RegionRoots.push_back(RegionX->getRegion());
1033 }
1034
1035 // Get the subregions of R. These are RegionRoots as well since they
1036 // represent values that are also bound to R.
1037 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1038 if (!SRptr) continue;
1039 SubRegionsTy SR = *SRptr;
1040
1041 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1042 RegionRoots.push_back(*I);
1043 }
1044
1045 // We have now scanned the store, marking reachable regions and symbols
1046 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001047 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001048 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1049 const MemRegion* R = I.getKey();
1050
1051 // If this region live? Is so, none of its symbols are dead.
1052 if (Marked.count(R))
1053 continue;
1054
1055 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001056 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001057
1058 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001059 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1060 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001061
1062 SVal X = I.getData();
1063 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001064 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001065 }
1066
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001067 return store;
1068}
1069
Zhongxing Xua071eb02008-10-24 06:01:33 +00001070void RegionStoreManager::print(Store store, std::ostream& Out,
1071 const char* nl, const char *sep) {
1072 llvm::raw_os_ostream OS(Out);
1073 RegionBindingsTy B = GetRegionBindings(store);
1074 OS << "Store:" << nl;
1075
1076 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1077 OS << ' '; I.getKey()->print(OS); OS << " : ";
1078 I.getData().print(OS); OS << nl;
1079 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001080}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001081
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001082const GRState* RegionStoreManager::BindArray(const GRState* St,
1083 const TypedRegion* R, SVal Init) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001084 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001085 assert(T->isArrayType());
1086
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001087 // When we are binding the whole array, it always has default value 0.
1088 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001089 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1090 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001091
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001092 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1093
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001094 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001095 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1096 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001097
1098 // Check if the init expr is a StringLiteral.
1099 if (isa<loc::MemRegionVal>(Init)) {
1100 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1101 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1102 const char* str = S->getStrData();
1103 unsigned len = S->getByteLength();
1104 unsigned j = 0;
1105
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001106 // Copy bytes from the string literal into the target array. Trailing bytes
1107 // in the array that are not covered by the string literal are initialized
1108 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001109 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001110 if (j >= len)
1111 break;
1112
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001113 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001114 ElementRegion* ER =
1115 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1116 Idx, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001117
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001118 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1119 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001120 }
1121
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001122 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001123 }
1124
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001125 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001126 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1127
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001128 for (; i < Size; ++i, ++VI) {
1129 // The init list might be shorter than the array decl.
1130 if (VI == VE)
1131 break;
1132
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001133 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001134 ElementRegion* ER =
1135 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1136 Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001137
1138 if (CAT->getElementType()->isStructureType())
1139 St = BindStruct(St, ER, *VI);
1140 else
1141 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001142 }
1143
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001144 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001145}
1146
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001147const GRState*
1148RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001149 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001150 assert(T->isStructureType());
1151
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001152 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001153 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001154
1155 if (!RD->isDefinition())
1156 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001157
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001158 if (V.isUnknown())
1159 return KillStruct(St, R);
1160
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001161 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001162 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001163 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1164 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001165
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001166 for (; FI != FE; ++FI, ++VI) {
1167
1168 // There may be fewer values than fields only when we are initializing a
1169 // struct decl. In this case, mark the region as having default value.
1170 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001171 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001172 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1173 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001174 break;
1175 }
1176
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001177 QualType FTy = (*FI)->getType();
1178 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1179
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001180 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1181 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001182
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001183 else if (FTy->isArrayType())
1184 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001185
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001186 else if (FTy->isStructureType())
1187 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001188 }
1189
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001190 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001191}
1192
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001193const GRState* RegionStoreManager::KillStruct(const GRState* St,
1194 const TypedRegion* R){
1195 GRStateRef state(St, StateMgr);
1196
1197 // Kill the struct region because it is assigned "unknown".
1198 St = state.add<RegionKills>(R);
1199
1200 // Set the default value of the struct region to "unknown".
1201 St = state.set<RegionDefaultValue>(R, UnknownVal());
1202
1203 Store store = St->getStore();
1204 RegionBindingsTy B = GetRegionBindings(store);
1205
1206 // Remove all bindings for the subregions of the struct.
1207 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1208 const MemRegion* r = I.getKey();
1209 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1210 if (sr->isSubRegionOf(R))
1211 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001212 // FIXME: Maybe we should also remove the bindings for the "views" of the
1213 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001214 }
1215
1216 return StateMgr.MakeStateWithStore(St, store);
1217}
1218
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001219const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1220 const MemRegion* View,
1221 const MemRegion* Base) {
1222 GRStateRef state(St, StateMgr);
1223
1224 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001225 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001226 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001227
1228 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001229 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001230
1231 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001232 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001233}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001234
1235const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1236 const MemRegion* View,
1237 const MemRegion* Base) {
1238 GRStateRef state(St, StateMgr);
1239
1240 // Retrieve the region view of the base region.
1241 const RegionViews* d = state.get<RegionViewMap>(Base);
1242
1243 // If the base region has no view, return.
1244 if (!d)
1245 return St;
1246
1247 // Remove the view.
1248 RegionViews V = *d;
1249 V = RVFactory.Remove(V, View);
1250
1251 return state.set<RegionViewMap>(Base, V);
1252}
Zhongxing Xu20794942009-05-06 08:33:50 +00001253
1254const GRState* RegionStoreManager::setCastType(const GRState* St,
1255 const MemRegion* R, QualType T) {
1256 GRStateRef state(St, StateMgr);
1257 return state.set<RegionCasts>(R, T);
1258}