blob: d20c70a64d5c1e9e7063b89cb44b3f115d7dcfe9 [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 Xu262fd032009-05-20 09:00:16 +0000217 SVal EvalBinOp(const GRState *state,BinaryOperator::Opcode Op,Loc L,NonLoc R);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000218
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 Xu264e9372009-05-12 10:10:00 +0000279 const GRState* setDefaultValue(const GRState* St, const MemRegion* R, SVal V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000280private:
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,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000441 BaseRegion, getContext()));
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
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000476 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
477 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000478}
479
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000480SVal RegionStoreManager::getSizeInElements(const GRState* St,
481 const MemRegion* R) {
482 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
483 // Get the type of the variable.
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000484 QualType T = VR->getDesugaredValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000485
Ted Kremenek14553ab2009-01-30 00:08:43 +0000486 // FIXME: Handle variable-length arrays.
487 if (isa<VariableArrayType>(T))
488 return UnknownVal();
489
490 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
491 // return the size as signed integer.
492 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
493 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000494
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000495 GRStateRef state(St, StateMgr);
496 const QualType* CastTy = state.get<RegionCasts>(VR);
497
Zhongxing Xue6536af2009-05-08 02:00:55 +0000498 // If the VarRegion is cast to other type, compute the size with respect to
499 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000500 if (CastTy) {
501 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000502 QualType VarTy = VR->getValueType(getContext());
Zhongxing Xue6536af2009-05-08 02:00:55 +0000503 uint64_t EleSize = getContext().getTypeSize(EleTy);
504 uint64_t VarSize = getContext().getTypeSize(VarTy);
505 return NonLoc::MakeIntVal(getBasicVals(), VarSize / EleSize, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000506 }
507
Ted Kremenek14553ab2009-01-30 00:08:43 +0000508 // Clients can use ordinary variables as if they were arrays. These
509 // essentially are arrays of size 1.
510 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000511 }
512
513 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000514 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000515 // We intentionally made the size value signed because it participates in
516 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000517 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000518 }
519
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000520 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
521 // FIXME: Unsupported yet.
522 FR = 0;
523 return UnknownVal();
524 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000525
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000526 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000527 return UnknownVal();
528 }
529
Zhongxing Xuce760782009-05-09 13:20:07 +0000530 if (isa<AllocaRegion>(R)) {
531 return UnknownVal();
532 }
533
Zhongxing Xuccb16162009-05-06 08:08:27 +0000534 if (isa<ElementRegion>(R)) {
535 return UnknownVal();
536 }
537
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000538 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000539 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000540}
541
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000542/// ArrayToPointer - Emulates the "decay" of an array to a pointer
543/// type. 'Array' represents the lvalue of the array being decayed
544/// to a pointer, and the returned SVal represents the decayed
545/// version of that lvalue (i.e., a pointer to the first element of
546/// the array). This is called by GRExprEngine when evaluating casts
547/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000548SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000549 if (!isa<loc::MemRegionVal>(Array))
550 return UnknownVal();
551
552 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
553 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
554
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000555 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000556 return UnknownVal();
557
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000558 // Strip off typedefs from the ArrayRegion's ValueType.
559 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000560 ArrayType *AT = cast<ArrayType>(T);
561 T = AT->getElementType();
562
Zhongxing Xu63123d82008-11-23 04:30:35 +0000563 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000564 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000565
566 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000567}
568
Zhongxing Xu88980592009-05-06 02:42:32 +0000569RegionStoreManager::CastResult
570RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000571 QualType CastToTy) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000572
573 ASTContext& Ctx = StateMgr.getContext();
574
575 // We need to know the real type of CastToTy.
576 QualType ToTy = Ctx.getCanonicalType(CastToTy);
577
578 // Check cast to ObjCQualifiedID type.
579 if (isa<ObjCQualifiedIdType>(ToTy)) {
580 // FIXME: Record the type information aside.
581 return CastResult(state, R);
582 }
583
584 // CodeTextRegion should be cast to only function pointer type.
585 if (isa<CodeTextRegion>(R)) {
586 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType());
587 return CastResult(state, R);
588 }
589
Zhongxing Xudb3a0982009-05-09 10:03:08 +0000590 // Now assume we are casting from pointer to pointer. Other cases should
591 // already be handled.
Zhongxing Xu88980592009-05-06 02:42:32 +0000592 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
593
Zhongxing Xu88980592009-05-06 02:42:32 +0000594 // Process region cast according to the kind of the region being cast.
595
Zhongxing Xu88980592009-05-06 02:42:32 +0000596 // FIXME: Need to handle arbitrary downcasts.
Zhongxing Xu88980592009-05-06 02:42:32 +0000597 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
Zhongxing Xuce760782009-05-09 13:20:07 +0000598 state = setCastType(state, R, ToTy);
599 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000600 }
601
602 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
603 // they should not be cast. We only layer an ElementRegion when the cast-to
604 // pointee type is of smaller size. In other cases, we return the original
605 // VarRegion.
606 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
607 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xu2572eda2009-05-08 07:28:25 +0000608 // If the pointee type is incomplete, do not compute its size, and return
609 // the original region.
610 if (const RecordType *RT = dyn_cast<RecordType>(PointeeTy.getTypePtr())) {
611 const RecordDecl *D = RT->getDecl();
612 if (!D->getDefinition(getContext()))
613 return CastResult(state, R);
614 }
615
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000616 QualType ObjTy = cast<TypedRegion>(R)->getValueType(getContext());
Zhongxing Xufb1e3312009-05-08 02:12:59 +0000617 uint64_t PointeeTySize = getContext().getTypeSize(PointeeTy);
618 uint64_t ObjTySize = getContext().getTypeSize(ObjTy);
619
Zhongxing Xu5bf32872009-05-09 15:34:29 +0000620 if ((PointeeTySize > 0 && PointeeTySize < ObjTySize) ||
621 (ObjTy->isAggregateType() && PointeeTy->isScalarType())) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000622 // Record the cast type of the region.
623 state = setCastType(state, R, ToTy);
624
Zhongxing Xuccb16162009-05-06 08:08:27 +0000625 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000626 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx,R,getContext());
Zhongxing Xuccb16162009-05-06 08:08:27 +0000627 return CastResult(state, ER);
628 } else
629 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000630 }
631
Zhongxing Xu88980592009-05-06 02:42:32 +0000632 if (isa<ObjCObjectRegion>(R)) {
633 return CastResult(state, R);
634 }
635
636 assert(0 && "Unprocessed region.");
Daniel Dunbar4e609002009-05-18 16:48:48 +0000637 return 0;
Zhongxing Xu88980592009-05-06 02:42:32 +0000638}
639
Zhongxing Xu262fd032009-05-20 09:00:16 +0000640SVal RegionStoreManager::EvalBinOp(const GRState *state,
641 BinaryOperator::Opcode Op, Loc L, NonLoc R) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000642 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000643 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000644 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000645
Zhongxing Xua1718c72009-04-03 07:33:13 +0000646 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000647 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000648
649 // If the operand is a symbolic or alloca region, create the first element
650 // region on it.
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000651 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR)) {
652 // Get symbol's type. It should be a pointer type.
653 SymbolRef Sym = SR->getSymbol();
654 QualType T = Sym->getType(getContext());
Zhongxing Xu53454dc2009-06-12 03:59:12 +0000655 QualType EleTy = T->getAsPointerType()->getPointeeType();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000656
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000657 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000658 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000659 }
660 else if (const AllocaRegion *AR = dyn_cast<AllocaRegion>(MR)) {
661 // Get the alloca region's current cast type.
662 GRStateRef StRef(state, StateMgr);
663
664 GRStateTrait<RegionCasts>::lookup_type T = StRef.get<RegionCasts>(AR);
665 assert(T && "alloca region has no type.");
666 QualType EleTy = cast<PointerType>(T->getTypePtr())->getPointeeType();
667 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000668 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Zhongxing Xu262fd032009-05-20 09:00:16 +0000669 }
670 else
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000671 ER = cast<ElementRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000672
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000673 SVal Idx = ER->getIndex();
674
675 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
676 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
677
678 // Only support concrete integer indexes for now.
679 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000680 // FIXME: For now, convert the signedness and bitwidth of offset in case
681 // they don't match. This can result from pointer arithmetic. In reality,
682 // we should figure out what are the proper semantics and implement them.
683 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000684 // This addresses the test case test/Analysis/ptr-arith.c
685 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000686 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
687 Offset->getValue()));
688 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000689 const MemRegion* NewER =
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000690 MRMgr.getElementRegion(ER->getElementType(), NewIdx,ER->getSuperRegion(),
691 getContext());
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000692 return Loc::MakeVal(NewER);
693
Ted Kremenek5dc27462009-03-03 02:51:43 +0000694 }
695
696 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000697}
698
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000699SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000700 assert(!isa<UnknownVal>(L) && "location unknown");
701 assert(!isa<UndefinedVal>(L) && "location undefined");
702
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000703 // FIXME: Is this even possible? Shouldn't this be treated as a null
704 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000705 if (isa<loc::ConcreteInt>(L))
706 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000707
Zhongxing Xua1718c72009-04-03 07:33:13 +0000708 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
709
Zhongxing Xu91844122009-05-20 09:18:48 +0000710 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000711 // Example:
712 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000713 // char* p = alloca();
714 // read(p);
715 // c = *p;
716 if (isa<SymbolicRegion>(MR) || isa<AllocaRegion>(MR))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000717 return UnknownVal();
718
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000719 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
720 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000721 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000722 assert(R && "bad region");
723
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000724 // FIXME: We should eventually handle funny addressing. e.g.:
725 //
726 // int x = ...;
727 // int *p = &x;
728 // char *q = (char*) p;
729 // char c = *q; // returns the first byte of 'x'.
730 //
731 // Such funny addressing will occur due to layering of regions.
732
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000733 QualType RTy = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000734
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000735 if (RTy->isStructureType())
736 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000737
738 if (RTy->isArrayType())
739 return RetrieveArray(St, R);
740
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000741 // FIXME: handle Vector types.
742 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000743 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000744
745 RegionBindingsTy B = GetRegionBindings(St->getStore());
746 RegionBindingsTy::data_type* V = B.lookup(R);
747
748 // Check if the region has a binding.
749 if (V)
750 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000751
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000752 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000753
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000754 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000755 if (state.contains<RegionKills>(R))
756 return UnknownVal();
757
Zhongxing Xuc87d5fb2009-05-11 14:23:36 +0000758 // Check if the region is an element region of a string literal.
759 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
760 if (const StringRegion *StrR=dyn_cast<StringRegion>(ER->getSuperRegion())) {
761 const StringLiteral *Str = StrR->getStringLiteral();
762 SVal Idx = ER->getIndex();
763 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
764 int64_t i = CI->getValue().getSExtValue();
765 char c;
766 if (i == Str->getByteLength())
767 c = '\0';
768 else
769 c = Str->getStrData()[i];
770 const llvm::APSInt &V = getBasicVals().getValue(c, getContext().CharTy);
771 return nonloc::ConcreteInt(V);
772 }
773 }
774 }
775
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000776 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000777 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
778 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
779 GRStateTrait<RegionDefaultValue>::lookup_type D =
780 state.get<RegionDefaultValue>(SuperR);
Zhongxing Xu264e9372009-05-12 10:10:00 +0000781 if (D) {
782 // If the default value is symbolic, we need to create a new symbol.
783 if (D->hasConjuredSymbol())
784 return ValMgr.getRegionValueSymbolVal(R);
785 else
786 return *D;
787 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000788 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000789
790 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
791 const MemRegion *SR = IVR->getSuperRegion();
792
793 // If the super region is 'self' then return the symbol representing
794 // the value of the ivar upon entry to the method.
795 if (SR == SelfRegion) {
796 // FIXME: Do we need to handle the case where the super region
797 // has a view? We want to canonicalize the bindings.
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000798 return ValMgr.getRegionValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000799 }
800
801 // Otherwise, we need a new symbol. For now return Unknown.
802 return UnknownVal();
803 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000804
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000805 // The location does not have a bound value. This means that it has
806 // the value it had upon its creation and/or entry to the analyzed
807 // function/method. These are either symbolic values or 'undefined'.
808
809 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000810 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
811 const VarDecl *VD = VR->getDecl();
812
Ted Kremenekcec35252009-03-04 00:23:05 +0000813 if (VD == SelfDecl)
814 return loc::MemRegionVal(getSelfRegion(0));
815
816 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
817 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000818 QualType VTy = VD->getType();
819 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000820 return ValMgr.getRegionValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000821 else
822 return UnknownVal();
823 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000824 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000825
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000826 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
827 // All stack variables are considered to have undefined values
828 // upon creation. All heap allocated blocks are considered to
829 // have undefined values as well unless they are explicitly bound
830 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000831 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000832 }
833
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000834 // All other integer values are symbolic.
835 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000836 return ValMgr.getRegionValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000837 else
838 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000839}
840
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000841SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000842 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000843 assert(T->isStructureType());
844
Zhongxing Xub7507d12009-06-11 07:27:30 +0000845 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000846 RecordDecl* RD = RT->getDecl();
847 assert(RD->isDefinition());
848
849 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
850
Douglas Gregor6ab35242009-04-09 21:40:53 +0000851 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
852 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000853
Douglas Gregore267ff32008-12-11 20:41:00 +0000854 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
855 FieldEnd = Fields.rend();
856 Field != FieldEnd; ++Field) {
857 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000858 QualType FTy = (*Field)->getType();
859 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000860 StructVal = getBasicVals().consVals(FieldValue, StructVal);
861 }
862
863 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
864}
865
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000866SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000867 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000868 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
869
870 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000871 llvm::APSInt Size(CAT->getSize(), false);
Zhongxing Xu2ee52142009-05-11 12:48:56 +0000872 llvm::APSInt i = getBasicVals().getZeroWithPtrWidth(false);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000873
874 for (; i < Size; ++i) {
875 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000876 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
877 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +0000878 QualType ETy = ER->getElementType();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000879 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
880 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
881 }
882
883 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
884}
885
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000886const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000887 // If we get here, the location should be a region.
888 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000889 assert(R);
890
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000891 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000892 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000893 if (TR->getValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000894 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000895
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000896 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000897 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000898
899 if (V.isUnknown()) {
900 // Remove the binding.
901 store = RBFactory.Remove(B, R).getRoot();
902
903 // Add the region to the killset.
904 GRStateRef state(St, StateMgr);
905 St = state.add<RegionKills>(R);
906 }
907 else
908 store = RBFactory.Add(B, R, V).getRoot();
909
910 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000911}
912
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000913Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000914 const MemRegion* R = 0;
915
916 if (isa<loc::MemRegionVal>(L))
917 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000918
919 if (R) {
920 RegionBindingsTy B = GetRegionBindings(store);
921 return RBFactory.Remove(B, R).getRoot();
922 }
923
924 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000925}
926
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000927const GRState* RegionStoreManager::BindDecl(const GRState* St,
928 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000929
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000930 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000931 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000932
Ted Kremenek0964a062009-01-21 06:57:53 +0000933 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000934 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +0000935 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000936 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +0000937
Ted Kremenek0964a062009-01-21 06:57:53 +0000938 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +0000939}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000940
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000941// FIXME: this method should be merged into Bind().
942const GRState*
943RegionStoreManager::BindCompoundLiteral(const GRState* St,
944 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000945 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000946 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000947}
948
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000949const GRState* RegionStoreManager::setExtent(const GRState* St,
950 const MemRegion* R, SVal Extent) {
951 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000952 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000953}
954
955
Ted Kremenek241677a2009-01-21 22:26:05 +0000956static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +0000957 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
958 const MemRegion *R = XR->getRegion();
959
960 while (R) {
961 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
962 SymReaper.markLive(SR->getSymbol());
963 return;
964 }
965
966 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
967 R = SR->getSuperRegion();
968 continue;
969 }
970
971 break;
972 }
973
974 return;
975 }
976
Ted Kremenek241677a2009-01-21 22:26:05 +0000977 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
978 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000979}
980
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000981Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000982 SymbolReaper& SymReaper,
983 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
984{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000985
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000986 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000987 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000988
989 // Lazily constructed backmap from MemRegions to SubRegions.
990 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
991 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
992
993 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
994 // the ability to reuse memory. This way we can keep TmpAlloc around as
995 // an instance variable of RegionStoreManager (avoiding repeated malloc
996 // overhead).
997 llvm::BumpPtrAllocator TmpAlloc;
998
999 // Factory objects.
1000 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1001 SubRegionsTy::Factory SubRegF(TmpAlloc);
1002
1003 // The backmap from regions to subregions.
1004 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1005
1006 // Do a pass over the regions in the store. For VarRegions we check if
1007 // the variable is still live and if so add it to the list of live roots.
1008 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001009
1010 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1011
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001012 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001013 IntermediateRoots.push_back(I.getKey());
1014 }
1015
1016 while (!IntermediateRoots.empty()) {
1017 const MemRegion* R = IntermediateRoots.back();
1018 IntermediateRoots.pop_back();
1019
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001020 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +00001021 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001022 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +00001023 }
1024 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1025 if (SymReaper.isLive(SR->getSymbol()))
1026 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001027 }
1028 else {
1029 // Get the super region for R.
1030 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001031
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001032 // Get the current set of subregions for SuperR.
1033 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
Zhongxing Xua3eda832009-05-08 23:28:07 +00001034 SubRegionsTy SRs = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001035
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001036 // Add R to the subregions of SuperR.
Zhongxing Xua3eda832009-05-08 23:28:07 +00001037 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SRs, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001038
1039 // Super region may be VarRegion or subregion of another VarRegion. Add it
1040 // to the work list.
1041 if (isa<SubRegion>(SuperR))
1042 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001043 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001044 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001045
1046 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1047 // of the store. We want to find all live symbols and dead regions.
1048 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1049
1050 while (!RegionRoots.empty()) {
1051 // Dequeue the next region on the worklist.
1052 const MemRegion* R = RegionRoots.back();
1053 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001054
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001055 // Check if we have already processed this region.
1056 if (Marked.count(R)) continue;
1057
1058 // Mark this region as processed. This is needed for termination in case
1059 // a region is referenced more than once.
1060 Marked.insert(R);
1061
1062 // Mark the symbol for any live SymbolicRegion as "live". This means we
1063 // should continue to track that symbol.
1064 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001065 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001066
1067 // Get the data binding for R (if any).
1068 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1069 if (Xptr) {
1070 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001071 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001072
1073 // If X is a region, then add it the RegionRoots.
1074 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1075 RegionRoots.push_back(RegionX->getRegion());
1076 }
1077
1078 // Get the subregions of R. These are RegionRoots as well since they
1079 // represent values that are also bound to R.
1080 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1081 if (!SRptr) continue;
1082 SubRegionsTy SR = *SRptr;
1083
1084 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1085 RegionRoots.push_back(*I);
1086 }
1087
1088 // We have now scanned the store, marking reachable regions and symbols
1089 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001090 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001091 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1092 const MemRegion* R = I.getKey();
1093
1094 // If this region live? Is so, none of its symbols are dead.
1095 if (Marked.count(R))
1096 continue;
1097
1098 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001099 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001100
1101 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001102 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1103 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001104
1105 SVal X = I.getData();
1106 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001107 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001108 }
1109
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001110 return store;
1111}
1112
Zhongxing Xua071eb02008-10-24 06:01:33 +00001113void RegionStoreManager::print(Store store, std::ostream& Out,
1114 const char* nl, const char *sep) {
1115 llvm::raw_os_ostream OS(Out);
1116 RegionBindingsTy B = GetRegionBindings(store);
1117 OS << "Store:" << nl;
1118
1119 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1120 OS << ' '; I.getKey()->print(OS); OS << " : ";
1121 I.getData().print(OS); OS << nl;
1122 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001123}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001124
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001125const GRState* RegionStoreManager::BindArray(const GRState* St,
1126 const TypedRegion* R, SVal Init) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001127 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001128 assert(T->isArrayType());
1129
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001130 // When we are binding the whole array, it always has default value 0.
1131 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001132 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1133 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001134
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001135 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1136
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001137 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001138 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1139 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001140
1141 // Check if the init expr is a StringLiteral.
1142 if (isa<loc::MemRegionVal>(Init)) {
1143 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1144 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1145 const char* str = S->getStrData();
1146 unsigned len = S->getByteLength();
1147 unsigned j = 0;
1148
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001149 // Copy bytes from the string literal into the target array. Trailing bytes
1150 // in the array that are not covered by the string literal are initialized
1151 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001152 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001153 if (j >= len)
1154 break;
1155
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001156 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001157 ElementRegion* ER =
1158 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001159 Idx, R, getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001160
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001161 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1162 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001163 }
1164
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001165 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001166 }
1167
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001168 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001169 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1170
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001171 for (; i < Size; ++i, ++VI) {
1172 // The init list might be shorter than the array decl.
1173 if (VI == VE)
1174 break;
1175
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001176 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001177 ElementRegion* ER =
1178 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001179 Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001180
1181 if (CAT->getElementType()->isStructureType())
1182 St = BindStruct(St, ER, *VI);
1183 else
1184 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001185 }
1186
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001187 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001188}
1189
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001190const GRState*
1191RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001192 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001193 assert(T->isStructureType());
1194
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001195 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001196 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001197
1198 if (!RD->isDefinition())
1199 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001200
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001201 if (V.isUnknown())
1202 return KillStruct(St, R);
1203
Zhongxing Xuf0ec39a2009-06-13 01:31:11 +00001204 // We may get non-CompoundVal accidentally due to imprecise cast logic. Ignore
1205 // them and make struct unknown.
1206 if (!isa<nonloc::CompoundVal>(V))
1207 return KillStruct(St, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001208
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001209 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001210 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001211 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1212 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001213
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001214 for (; FI != FE; ++FI, ++VI) {
1215
1216 // There may be fewer values than fields only when we are initializing a
1217 // struct decl. In this case, mark the region as having default value.
1218 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001219 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001220 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1221 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001222 break;
1223 }
1224
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001225 QualType FTy = (*FI)->getType();
1226 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1227
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001228 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1229 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001230
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001231 else if (FTy->isArrayType())
1232 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001233
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001234 else if (FTy->isStructureType())
1235 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001236 }
1237
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001238 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001239}
1240
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001241const GRState* RegionStoreManager::KillStruct(const GRState* St,
1242 const TypedRegion* R){
1243 GRStateRef state(St, StateMgr);
1244
1245 // Kill the struct region because it is assigned "unknown".
1246 St = state.add<RegionKills>(R);
1247
1248 // Set the default value of the struct region to "unknown".
1249 St = state.set<RegionDefaultValue>(R, UnknownVal());
1250
1251 Store store = St->getStore();
1252 RegionBindingsTy B = GetRegionBindings(store);
1253
1254 // Remove all bindings for the subregions of the struct.
1255 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1256 const MemRegion* r = I.getKey();
1257 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1258 if (sr->isSubRegionOf(R))
1259 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001260 // FIXME: Maybe we should also remove the bindings for the "views" of the
1261 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001262 }
1263
1264 return StateMgr.MakeStateWithStore(St, store);
1265}
1266
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001267const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1268 const MemRegion* View,
1269 const MemRegion* Base) {
1270 GRStateRef state(St, StateMgr);
1271
1272 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001273 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001274 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001275
1276 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001277 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001278
1279 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001280 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001281}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001282
1283const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1284 const MemRegion* View,
1285 const MemRegion* Base) {
1286 GRStateRef state(St, StateMgr);
1287
1288 // Retrieve the region view of the base region.
1289 const RegionViews* d = state.get<RegionViewMap>(Base);
1290
1291 // If the base region has no view, return.
1292 if (!d)
1293 return St;
1294
1295 // Remove the view.
1296 RegionViews V = *d;
1297 V = RVFactory.Remove(V, View);
1298
1299 return state.set<RegionViewMap>(Base, V);
1300}
Zhongxing Xu20794942009-05-06 08:33:50 +00001301
1302const GRState* RegionStoreManager::setCastType(const GRState* St,
1303 const MemRegion* R, QualType T) {
1304 GRStateRef state(St, StateMgr);
1305 return state.set<RegionCasts>(R, T);
1306}
Zhongxing Xu264e9372009-05-12 10:10:00 +00001307
1308const GRState* RegionStoreManager::setDefaultValue(const GRState* St,
1309 const MemRegion* R, SVal V) {
1310 GRStateRef state(St, StateMgr);
1311 return state.set<RegionDefaultValue>(R, V);
1312}