blob: 5f987fdf27154fe80d3c940dee5c8efe960864bf [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 Xu41fd0182009-05-06 11:51:48 +0000300 TargetInfo& getTargetInfo() { return getContext().getTargetInfo(); }
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
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000315// getTypeWidth - compute the width of the type. Should pass in
316// canonical type.
317static unsigned getTypeWidth(ASTContext& Ctx, QualType T) {
318 TargetInfo& Target = Ctx.getTargetInfo();
319 QualType CanT = Ctx.getCanonicalType(T);
320
321 if (CanT->isPointerType())
322 return Target.getPointerWidth(0);
323
324 if (CanT->isCharType())
325 return Target.getCharWidth();
326
327 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanT)) {
328 switch (BT->getKind()) {
329 case BuiltinType::Char_U:
330 case BuiltinType::UChar:
331 case BuiltinType::Char_S:
332 case BuiltinType::SChar:
333 return Target.getCharWidth();
334
335 case BuiltinType::UShort:
336 case BuiltinType::Short:
337 return Target.getShortWidth();
338
339 case BuiltinType::UInt:
340 case BuiltinType::Int:
341 return Target.getIntWidth();
342
343 case BuiltinType::ULong:
344 case BuiltinType::Long:
345 return Target.getLongWidth();
346 default:
347 assert(0 && "Unprocessed builtin type.");
348 }
349 }
350
351 assert(0 && "Unprocessed type.");
352}
353
Ted Kremenek14453bf2009-03-03 19:02:42 +0000354SubRegionMap* RegionStoreManager::getSubRegionMap(const GRState *state) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000355 RegionBindingsTy B = GetRegionBindings(state->getStore());
356 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
357
358 for (RegionBindingsTy::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
359 if (const SubRegion* R = dyn_cast<SubRegion>(I.getKey()))
360 M->add(R->getSuperRegion(), R);
361 }
362
Ted Kremenek14453bf2009-03-03 19:02:42 +0000363 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000364}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000365
366/// getLValueString - Returns an SVal representing the lvalue of a
367/// StringLiteral. Within RegionStore a StringLiteral has an
368/// associated StringRegion, and the lvalue of a StringLiteral is the
369/// lvalue of that region.
Zhongxing Xu143bf822008-10-25 14:18:57 +0000370SVal RegionStoreManager::getLValueString(const GRState* St,
371 const StringLiteral* S) {
372 return loc::MemRegionVal(MRMgr.getStringRegion(S));
373}
374
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000375/// getLValueVar - Returns an SVal that represents the lvalue of a
376/// variable. Within RegionStore a variable has an associated
377/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000378SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
379 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
380}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000381
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000382/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
383/// of a compound literal. Within RegionStore a compound literal
384/// has an associated region, and the lvalue of the compound literal
385/// is the lvalue of that region.
386SVal
387RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
388 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000389 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
390}
391
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000392SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
393 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000394 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000395}
396
397SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
398 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000399 return getLValueFieldOrIvar(St, Base, D);
400}
401
402SVal RegionStoreManager::getLValueFieldOrIvar(const GRState* St, SVal Base,
403 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000404 if (Base.isUnknownOrUndef())
405 return Base;
406
407 Loc BaseL = cast<Loc>(Base);
408 const MemRegion* BaseR = 0;
409
410 switch (BaseL.getSubKind()) {
411 case loc::MemRegionKind:
412 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000413 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(BaseR)) {
414 SymbolRef Sym = SR->getSymbol();
415 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
416 }
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000417 break;
418
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000419 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000420 // These are anormal cases. Flag an undefined value.
421 return UndefinedVal();
422
423 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000424 // While these seem funny, this can happen through casts.
425 // FIXME: What we should return is the field offset. For example,
426 // add the field offset to the integer value. That way funny things
427 // like this work properly: &(((struct foo *) 0xa)->f)
428 return Base;
429
430 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000431 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000432 return Base;
433 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000434
435 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
436 // of FieldDecl.
437 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
438 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000439
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000440 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000441}
442
Ted Kremenekf936f452009-05-04 06:18:28 +0000443SVal RegionStoreManager::getLValueElement(const GRState* St,
444 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000445 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000446
Ted Kremenekde7ec632009-03-09 22:44:49 +0000447 // If the base is an unknown or undefined value, just return it back.
448 // FIXME: For absolute pointer addresses, we just return that value back as
449 // well, although in reality we should return the offset added to that
450 // value.
451 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000452 return Base;
453
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000454 // Only handle integer offsets... for now.
455 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000456 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000457
Zhongxing Xua48f7372009-02-06 08:44:27 +0000458 const TypedRegion* BaseRegion = 0;
459
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000460 const MemRegion* R = cast<loc::MemRegionVal>(Base).getRegion();
461 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
462 SymbolRef Sym = SR->getSymbol();
Ted Kremeneke8e86482009-03-30 22:20:54 +0000463 BaseRegion = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000464 }
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000465 else
466 BaseRegion = cast<TypedRegion>(R);
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000467
468 // Pointer of any type can be cast and used as array base.
469 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
470
471 if (!ElemR) {
472 //
473 // If the base region is not an ElementRegion, create one.
474 // This can happen in the following example:
475 //
476 // char *p = __builtin_alloc(10);
477 // p[1] = 8;
478 //
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000479 // Observe that 'p' binds to an TypedViewRegion<AllocaRegion>.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000480 //
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000481
482 // Offset might be unsigned. We have to convert it to signed ConcreteInt.
483 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&Offset)) {
484 const llvm::APSInt& OffI = CI->getValue();
485 if (OffI.isUnsigned()) {
486 llvm::APSInt Tmp = OffI;
487 Tmp.setIsSigned(true);
488 Offset = NonLoc::MakeVal(getBasicVals(), Tmp);
489 }
490 }
Ted Kremenekf936f452009-05-04 06:18:28 +0000491 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
492 BaseRegion));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000493 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000494
495 SVal BaseIdx = ElemR->getIndex();
496
497 if (!isa<nonloc::ConcreteInt>(BaseIdx))
498 return UnknownVal();
499
500 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
501 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
502 assert(BaseIdxI.isSigned());
503
504 // FIXME: This appears to be the assumption of this code. We should review
505 // whether or not BaseIdxI.getBitWidth() < OffI.getBitWidth(). If it
506 // can't we need to put a comment here. If it can, we should handle it.
507 assert(BaseIdxI.getBitWidth() >= OffI.getBitWidth());
Zhongxing Xue4d13932008-11-13 09:48:44 +0000508
Ted Kremenekf936f452009-05-04 06:18:28 +0000509 const TypedRegion *ArrayR = cast<TypedRegion>(ElemR->getSuperRegion());
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000510 SVal NewIdx;
511
512 if (OffI.isUnsigned() || OffI.getBitWidth() < BaseIdxI.getBitWidth()) {
513 // 'Offset' might be unsigned. We have to convert it to signed and
514 // possibly extend it.
515 llvm::APSInt Tmp = OffI;
516
517 if (OffI.getBitWidth() < BaseIdxI.getBitWidth())
518 Tmp.extend(BaseIdxI.getBitWidth());
519
520 Tmp.setIsSigned(true);
521 Tmp += BaseIdxI; // Compute the new offset.
Zhongxing Xu5ea2ffc2009-02-19 08:37:16 +0000522 NewIdx = NonLoc::MakeVal(getBasicVals(), Tmp);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000523 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000524 else
525 NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000526
Ted Kremenekf936f452009-05-04 06:18:28 +0000527 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000528}
529
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000530SVal RegionStoreManager::getSizeInElements(const GRState* St,
531 const MemRegion* R) {
532 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
533 // Get the type of the variable.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000534 QualType T = VR->getDesugaredRValueType(getContext());
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000535
Ted Kremenek14553ab2009-01-30 00:08:43 +0000536 // FIXME: Handle variable-length arrays.
537 if (isa<VariableArrayType>(T))
538 return UnknownVal();
539
540 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
541 // return the size as signed integer.
542 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
543 }
Zhongxing Xu20794942009-05-06 08:33:50 +0000544
545 // If the VarRegion is cast to other type, compute the size with respect to
546 // that type.
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000547 GRStateRef state(St, StateMgr);
548 const QualType* CastTy = state.get<RegionCasts>(VR);
549
550 if (CastTy) {
551 QualType EleTy =cast<PointerType>(CastTy->getTypePtr())->getPointeeType();
552 QualType VarTy = VR->getRValueType(getContext());
553 unsigned EleWidth = getTypeWidth(getContext(), EleTy);
554 unsigned VarWidth = getTypeWidth(getContext(), VarTy);
555 return NonLoc::MakeIntVal(getBasicVals(), VarWidth / EleWidth, false);
556 }
557
Ted Kremenek14553ab2009-01-30 00:08:43 +0000558 // Clients can use ordinary variables as if they were arrays. These
559 // essentially are arrays of size 1.
560 return NonLoc::MakeIntVal(getBasicVals(), 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000561 }
562
563 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000564 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000565 // We intentionally made the size value signed because it participates in
566 // operations with signed indices.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000567 return NonLoc::MakeIntVal(getBasicVals(), Str->getByteLength()+1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000568 }
569
Ted Kremenek0312c0e2009-03-01 05:44:08 +0000570 if (const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek2e842572009-01-22 23:56:56 +0000571#if 0
572 // FIXME: This logic doesn't really work, as we can have all sorts of
573 // weird cases. For example, this crashes on test case 'rdar-6442306-1.m'.
574 // The weird cases come in when arbitrary casting comes into play, violating
575 // any type-safe programming.
576
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000577 GRStateRef state(St, StateMgr);
578
579 // Get the size of the super region in bytes.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000580 const SVal* Extent = state.get<RegionExtents>(ATR->getSuperRegion());
581 assert(Extent && "region extent not exist");
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000582
583 // Assume it's ConcreteInt for now.
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000584 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*Extent).getValue();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000585
586 // Get the size of the element in bits.
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000587 QualType LvT = ATR->getLValueType(getContext());
588 QualType ElemTy = cast<PointerType>(LvT.getTypePtr())->getPointeeType();
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000589
590 uint64_t X = getContext().getTypeSize(ElemTy);
591
592 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
593 false);
594
595 // Calculate the number of elements.
596
597 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
598 // signed?
599 if (SSize.isUnsigned())
600 SSize.setIsSigned(true);
601
602 // FIXME: move this operation into BasicVals.
603 const llvm::APSInt S =
604 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
605
606 return NonLoc::MakeVal(getBasicVals(), S);
Ted Kremenek2e842572009-01-22 23:56:56 +0000607#else
608 ATR = ATR;
609 return UnknownVal();
610#endif
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000611 }
612
613 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
614 // FIXME: Unsupported yet.
615 FR = 0;
616 return UnknownVal();
617 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000618
Zhongxing Xu02ebefb2009-02-06 08:51:30 +0000619 if (isa<SymbolicRegion>(R)) {
Zhongxing Xua48f7372009-02-06 08:44:27 +0000620 return UnknownVal();
621 }
622
Zhongxing Xuccb16162009-05-06 08:08:27 +0000623 if (isa<ElementRegion>(R)) {
624 return UnknownVal();
625 }
626
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000627 assert(0 && "Other regions are not supported yet.");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000628 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000629}
630
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000631/// ArrayToPointer - Emulates the "decay" of an array to a pointer
632/// type. 'Array' represents the lvalue of the array being decayed
633/// to a pointer, and the returned SVal represents the decayed
634/// version of that lvalue (i.e., a pointer to the first element of
635/// the array). This is called by GRExprEngine when evaluating casts
636/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000637SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000638 if (!isa<loc::MemRegionVal>(Array))
639 return UnknownVal();
640
641 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
642 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
643
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000644 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000645 return UnknownVal();
646
Ted Kremenekf936f452009-05-04 06:18:28 +0000647 // Strip off typedefs from the ArrayRegion's RvalueType.
648 QualType T = ArrayR->getRValueType(getContext())->getDesugaredType();
649 ArrayType *AT = cast<ArrayType>(T);
650 T = AT->getElementType();
651
Zhongxing Xu63123d82008-11-23 04:30:35 +0000652 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000653 ElementRegion* ER = MRMgr.getElementRegion(T, Idx, ArrayR);
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000654
655 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000656}
657
Zhongxing Xuccb16162009-05-06 08:08:27 +0000658static bool isSmallerThan(QualType T1, QualType T2) {
659 if (T1->isCharType())
660 return true;
661 else
662 return false;
663}
664
Zhongxing Xu88980592009-05-06 02:42:32 +0000665RegionStoreManager::CastResult
666RegionStoreManager::CastRegion(const GRState* state, const MemRegion* R,
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000667 QualType CastToTy) {
Zhongxing Xu88980592009-05-06 02:42:32 +0000668
669 ASTContext& Ctx = StateMgr.getContext();
670
671 // We need to know the real type of CastToTy.
672 QualType ToTy = Ctx.getCanonicalType(CastToTy);
673
674 // Check cast to ObjCQualifiedID type.
675 if (isa<ObjCQualifiedIdType>(ToTy)) {
676 // FIXME: Record the type information aside.
677 return CastResult(state, R);
678 }
679
680 // CodeTextRegion should be cast to only function pointer type.
681 if (isa<CodeTextRegion>(R)) {
682 assert(CastToTy->isFunctionPointerType() || CastToTy->isBlockPointerType());
683 return CastResult(state, R);
684 }
685
686 // Assume we are casting from pointer to pointer. Other cases are handled
687 // elsewhere.
688 QualType PointeeTy = cast<PointerType>(ToTy.getTypePtr())->getPointeeType();
689
690 // Return the same region if the region types are compatible.
691 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
692 QualType Ta = Ctx.getCanonicalType(TR->getLValueType(Ctx));
693
694 if (Ta == ToTy)
695 return CastResult(state, R);
696 }
697
698 // Process region cast according to the kind of the region being cast.
699
700
701 // FIXME: Need to handle arbitrary downcasts.
702 // FIXME: Handle the case where a TypedViewRegion (layering a SymbolicRegion
703 // or an AllocaRegion is cast to another view, thus causing the memory
704 // to be re-used for a different purpose.
705
706 if (isa<SymbolicRegion>(R) || isa<AllocaRegion>(R)) {
707 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
708 return CastResult(AddRegionView(state, ViewR, R), ViewR);
709 }
710
711 // VarRegion, ElementRegion, and FieldRegion has an inherent type. Normally
712 // they should not be cast. We only layer an ElementRegion when the cast-to
713 // pointee type is of smaller size. In other cases, we return the original
714 // VarRegion.
715 if (isa<VarRegion>(R) || isa<ElementRegion>(R) || isa<FieldRegion>(R)
716 || isa<ObjCIvarRegion>(R) || isa<CompoundLiteralRegion>(R)) {
Zhongxing Xuccb16162009-05-06 08:08:27 +0000717 if (isSmallerThan(PointeeTy,
718 cast<TypedRegion>(R)->getRValueType(getContext()))) {
Zhongxing Xu20794942009-05-06 08:33:50 +0000719 // Record the cast type of the region.
720 state = setCastType(state, R, ToTy);
721
Zhongxing Xuccb16162009-05-06 08:08:27 +0000722 SVal Idx = ValMgr.makeZeroArrayIndex();
Zhongxing Xu20794942009-05-06 08:33:50 +0000723 ElementRegion* ER = MRMgr.getElementRegion(PointeeTy, Idx, R);
Zhongxing Xuccb16162009-05-06 08:08:27 +0000724 return CastResult(state, ER);
725 } else
726 return CastResult(state, R);
Zhongxing Xu88980592009-05-06 02:42:32 +0000727 }
728
729 if (isa<TypedViewRegion>(R)) {
730 const MemRegion* ViewR = MRMgr.getTypedViewRegion(CastToTy, R);
731 return CastResult(state, ViewR);
732 }
733
734 if (isa<ObjCObjectRegion>(R)) {
735 return CastResult(state, R);
736 }
737
738 assert(0 && "Unprocessed region.");
739}
740
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000741SVal RegionStoreManager::EvalBinOp(BinaryOperator::Opcode Op, Loc L, NonLoc R) {
742 // Assume the base location is MemRegionVal(ElementRegion).
Ted Kremenek5dc27462009-03-03 02:51:43 +0000743 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000744 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000745
Zhongxing Xua1718c72009-04-03 07:33:13 +0000746 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
747 if (isa<SymbolicRegion>(MR))
748 return UnknownVal();
749
750 const TypedRegion* TR = cast<TypedRegion>(MR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000751 const ElementRegion* ER = dyn_cast<ElementRegion>(TR);
752
753 if (!ER) {
754 // If the region is not element region, create one with index 0. This can
755 // happen in the following example:
756 // char *p = foo();
757 // p += 3;
758 // Note that p binds to a TypedViewRegion(SymbolicRegion).
759 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Ted Kremenekf936f452009-05-04 06:18:28 +0000760 ER = MRMgr.getElementRegion(TR->getRValueType(getContext()), Idx, TR);
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000761 }
762
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000763 SVal Idx = ER->getIndex();
764
765 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
766 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
767
768 // Only support concrete integer indexes for now.
769 if (Base && Offset) {
Ted Kremenek610e81d2009-03-13 15:35:24 +0000770 // FIXME: For now, convert the signedness and bitwidth of offset in case
771 // they don't match. This can result from pointer arithmetic. In reality,
772 // we should figure out what are the proper semantics and implement them.
773 //
Ted Kremenek1060a3c2009-03-13 15:39:16 +0000774 // This addresses the test case test/Analysis/ptr-arith.c
775 //
Ted Kremenek610e81d2009-03-13 15:35:24 +0000776 nonloc::ConcreteInt OffConverted(getBasicVals().Convert(Base->getValue(),
777 Offset->getValue()));
778 SVal NewIdx = Base->EvalBinOp(getBasicVals(), Op, OffConverted);
Ted Kremenekf936f452009-05-04 06:18:28 +0000779 const MemRegion* NewER =
780 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
781 cast<TypedRegion>(ER->getSuperRegion()));
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000782 return Loc::MakeVal(NewER);
783
Ted Kremenek5dc27462009-03-03 02:51:43 +0000784 }
785
786 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000787}
788
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000789SVal RegionStoreManager::Retrieve(const GRState* St, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000790 assert(!isa<UnknownVal>(L) && "location unknown");
791 assert(!isa<UndefinedVal>(L) && "location undefined");
792
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000793 // FIXME: Is this even possible? Shouldn't this be treated as a null
794 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000795 if (isa<loc::ConcreteInt>(L))
796 return UndefinedVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000797
Zhongxing Xua1718c72009-04-03 07:33:13 +0000798 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
799
800 // We return unknown for symbolic region for now. This might be improved.
801 // Example:
802 // void f(int* p) { int x = *p; }
803 if (isa<SymbolicRegion>(MR))
804 return UnknownVal();
805
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000806 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
807 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000808 const TypedRegion* R = cast<TypedRegion>(MR);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000809 assert(R && "bad region");
810
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000811 // FIXME: We should eventually handle funny addressing. e.g.:
812 //
813 // int x = ...;
814 // int *p = &x;
815 // char *q = (char*) p;
816 // char c = *q; // returns the first byte of 'x'.
817 //
818 // Such funny addressing will occur due to layering of regions.
819
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000820 QualType RTy = R->getRValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000821
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000822 if (RTy->isStructureType())
823 return RetrieveStruct(St, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000824
825 if (RTy->isArrayType())
826 return RetrieveArray(St, R);
827
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000828 // FIXME: handle Vector types.
829 if (RTy->isVectorType())
Ted Kremenek265a3052009-02-24 02:23:11 +0000830 return UnknownVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000831
832 RegionBindingsTy B = GetRegionBindings(St->getStore());
833 RegionBindingsTy::data_type* V = B.lookup(R);
834
835 // Check if the region has a binding.
836 if (V)
837 return *V;
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000838
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000839 GRStateRef state(St, StateMgr);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000840
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000841 // Check if the region is in killset.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000842 if (state.contains<RegionKills>(R))
843 return UnknownVal();
844
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000845 // If the region is an element or field, it may have a default value.
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000846 if (isa<ElementRegion>(R) || isa<FieldRegion>(R)) {
847 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
848 GRStateTrait<RegionDefaultValue>::lookup_type D =
849 state.get<RegionDefaultValue>(SuperR);
850 if (D)
851 return *D;
852 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000853
854 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R)) {
855 const MemRegion *SR = IVR->getSuperRegion();
856
857 // If the super region is 'self' then return the symbol representing
858 // the value of the ivar upon entry to the method.
859 if (SR == SelfRegion) {
860 // FIXME: Do we need to handle the case where the super region
861 // has a view? We want to canonicalize the bindings.
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000862 return ValMgr.getRValueSymbolVal(R);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000863 }
864
865 // Otherwise, we need a new symbol. For now return Unknown.
866 return UnknownVal();
867 }
Zhongxing Xu562c4d92009-01-23 11:22:12 +0000868
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000869 // The location does not have a bound value. This means that it has
870 // the value it had upon its creation and/or entry to the analyzed
871 // function/method. These are either symbolic values or 'undefined'.
872
873 // We treat function parameters as symbolic values.
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000874 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
875 const VarDecl *VD = VR->getDecl();
876
Ted Kremenekcec35252009-03-04 00:23:05 +0000877 if (VD == SelfDecl)
878 return loc::MemRegionVal(getSelfRegion(0));
879
880 if (isa<ParmVarDecl>(VD) || isa<ImplicitParamDecl>(VD) ||
881 VD->hasGlobalStorage()) {
Zhongxing Xud8895f62009-02-19 09:56:08 +0000882 QualType VTy = VD->getType();
883 if (Loc::IsLocType(VTy) || VTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000884 return ValMgr.getRValueSymbolVal(VR);
Zhongxing Xud8895f62009-02-19 09:56:08 +0000885 else
886 return UnknownVal();
887 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000888 }
Ted Kremenek265a3052009-02-24 02:23:11 +0000889
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000890 if (MRMgr.onStack(R) || MRMgr.onHeap(R)) {
891 // All stack variables are considered to have undefined values
892 // upon creation. All heap allocated blocks are considered to
893 // have undefined values as well unless they are explicitly bound
894 // to specific values.
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000895 return UndefinedVal();
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000896 }
897
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000898 // All other integer values are symbolic.
899 if (Loc::IsLocType(RTy) || RTy->isIntegerType())
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000900 return ValMgr.getRValueSymbolVal(R);
Zhongxing Xuff8d2042009-03-09 09:31:22 +0000901 else
902 return UnknownVal();
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000903}
904
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000905SVal RegionStoreManager::RetrieveStruct(const GRState* St,const TypedRegion* R){
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000906 // FIXME: Verify we want getRValueType instead of getLValueType.
907 QualType T = R->getRValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000908 assert(T->isStructureType());
909
910 const RecordType* RT = cast<RecordType>(T.getTypePtr());
911 RecordDecl* RD = RT->getDecl();
912 assert(RD->isDefinition());
913
914 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
915
Douglas Gregor6ab35242009-04-09 21:40:53 +0000916 std::vector<FieldDecl *> Fields(RD->field_begin(getContext()),
917 RD->field_end(getContext()));
Douglas Gregor44b43212008-12-11 16:49:14 +0000918
Douglas Gregore267ff32008-12-11 20:41:00 +0000919 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
920 FieldEnd = Fields.rend();
921 Field != FieldEnd; ++Field) {
922 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000923 QualType FTy = (*Field)->getType();
924 SVal FieldValue = Retrieve(St, loc::MemRegionVal(FR), FTy);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000925 StructVal = getBasicVals().consVals(FieldValue, StructVal);
926 }
927
928 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
929}
930
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000931SVal RegionStoreManager::RetrieveArray(const GRState* St, const TypedRegion* R){
932 QualType T = R->getRValueType(getContext());
933 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
934
935 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000936 llvm::APSInt Size(CAT->getSize(), false);
937 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
938 Size.isUnsigned());
939
940 for (; i < Size; ++i) {
941 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +0000942 ElementRegion* ER = MRMgr.getElementRegion(R->getRValueType(getContext()),
943 Idx, R);
944 QualType ETy = ER->getElementType();
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000945 SVal ElementVal = Retrieve(St, loc::MemRegionVal(ER), ETy);
946 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
947 }
948
949 return NonLoc::MakeCompoundVal(T, ArrayVal, getBasicVals());
950}
951
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000952const GRState* RegionStoreManager::Bind(const GRState* St, Loc L, SVal V) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000953 // If we get here, the location should be a region.
954 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000955 assert(R);
956
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000957 // Check if the region is a struct region.
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000958 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000959 // FIXME: Verify we want getRValueType().
960 if (TR->getRValueType(getContext())->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000961 return BindStruct(St, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000962
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000963 Store store = St->getStore();
Zhongxing Xu17892752008-10-08 02:50:44 +0000964 RegionBindingsTy B = GetRegionBindings(store);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000965
966 if (V.isUnknown()) {
967 // Remove the binding.
968 store = RBFactory.Remove(B, R).getRoot();
969
970 // Add the region to the killset.
971 GRStateRef state(St, StateMgr);
972 St = state.add<RegionKills>(R);
973 }
974 else
975 store = RBFactory.Add(B, R, V).getRoot();
976
977 return StateMgr.MakeStateWithStore(St, store);
Zhongxing Xu17892752008-10-08 02:50:44 +0000978}
979
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000980Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +0000981 const MemRegion* R = 0;
982
983 if (isa<loc::MemRegionVal>(L))
984 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +0000985
986 if (R) {
987 RegionBindingsTy B = GetRegionBindings(store);
988 return RBFactory.Remove(B, R).getRoot();
989 }
990
991 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +0000992}
993
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000994const GRState* RegionStoreManager::BindDecl(const GRState* St,
995 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000996
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000997 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000998 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000999
Ted Kremenek0964a062009-01-21 06:57:53 +00001000 if (T->isArrayType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001001 return BindArray(St, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001002 if (T->isStructureType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001003 return BindStruct(St, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001004
Ted Kremenek0964a062009-01-21 06:57:53 +00001005 return Bind(St, Loc::MakeVal(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001006}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001007
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001008// FIXME: this method should be merged into Bind().
1009const GRState*
1010RegionStoreManager::BindCompoundLiteral(const GRState* St,
1011 const CompoundLiteralExpr* CL, SVal V) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001012 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001013 return Bind(St, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001014}
1015
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001016const GRState* RegionStoreManager::setExtent(const GRState* St,
1017 const MemRegion* R, SVal Extent) {
1018 GRStateRef state(St, StateMgr);
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001019 return state.set<RegionExtents>(R, Extent);
Zhongxing Xubaf03a72008-11-24 09:44:56 +00001020}
1021
1022
Ted Kremenek241677a2009-01-21 22:26:05 +00001023static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
Ted Kremenek02c4d2d2009-03-04 00:11:38 +00001024 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1025 const MemRegion *R = XR->getRegion();
1026
1027 while (R) {
1028 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1029 SymReaper.markLive(SR->getSymbol());
1030 return;
1031 }
1032
1033 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1034 R = SR->getSuperRegion();
1035 continue;
1036 }
1037
1038 break;
1039 }
1040
1041 return;
1042 }
1043
Ted Kremenek241677a2009-01-21 22:26:05 +00001044 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1045 SymReaper.markLive(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001046}
1047
Ted Kremenek2ed14be2008-12-05 00:47:52 +00001048Store RegionStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +00001049 SymbolReaper& SymReaper,
1050 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
1051{
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001052
Ted Kremenek2ed14be2008-12-05 00:47:52 +00001053 Store store = state->getStore();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001054 RegionBindingsTy B = GetRegionBindings(store);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001055
1056 // Lazily constructed backmap from MemRegions to SubRegions.
1057 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1058 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1059
1060 // FIXME: As a future optimization we can modifiy BumpPtrAllocator to have
1061 // the ability to reuse memory. This way we can keep TmpAlloc around as
1062 // an instance variable of RegionStoreManager (avoiding repeated malloc
1063 // overhead).
1064 llvm::BumpPtrAllocator TmpAlloc;
1065
1066 // Factory objects.
1067 SubRegionsMapTy::Factory SubRegMapF(TmpAlloc);
1068 SubRegionsTy::Factory SubRegF(TmpAlloc);
1069
1070 // The backmap from regions to subregions.
1071 SubRegionsMapTy SubRegMap = SubRegMapF.GetEmptyMap();
1072
1073 // Do a pass over the regions in the store. For VarRegions we check if
1074 // the variable is still live and if so add it to the list of live roots.
1075 // For other regions we populate our region backmap.
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001076
1077 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1078
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001079 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001080 IntermediateRoots.push_back(I.getKey());
1081 }
1082
1083 while (!IntermediateRoots.empty()) {
1084 const MemRegion* R = IntermediateRoots.back();
1085 IntermediateRoots.pop_back();
1086
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001087 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek241677a2009-01-21 22:26:05 +00001088 if (SymReaper.isLive(Loc, VR->getDecl()))
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001089 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu5c86b192009-04-29 09:24:35 +00001090 }
1091 else if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
1092 if (SymReaper.isLive(SR->getSymbol()))
1093 RegionRoots.push_back(SR);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001094 }
1095 else {
1096 // Get the super region for R.
1097 const MemRegion* SuperR = cast<SubRegion>(R)->getSuperRegion();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001098
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001099 // Get the current set of subregions for SuperR.
1100 const SubRegionsTy* SRptr = SubRegMap.lookup(SuperR);
1101 SubRegionsTy SR = SRptr ? *SRptr : SubRegF.GetEmptySet();
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001102
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001103 // Add R to the subregions of SuperR.
1104 SubRegMap = SubRegMapF.Add(SubRegMap, SuperR, SubRegF.Add(SR, R));
Zhongxing Xuc2fdb072009-03-18 01:54:31 +00001105
1106 // Super region may be VarRegion or subregion of another VarRegion. Add it
1107 // to the work list.
1108 if (isa<SubRegion>(SuperR))
1109 IntermediateRoots.push_back(SuperR);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001110 }
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001111 }
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001112
1113 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1114 // of the store. We want to find all live symbols and dead regions.
1115 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
1116
1117 while (!RegionRoots.empty()) {
1118 // Dequeue the next region on the worklist.
1119 const MemRegion* R = RegionRoots.back();
1120 RegionRoots.pop_back();
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001121
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001122 // Check if we have already processed this region.
1123 if (Marked.count(R)) continue;
1124
1125 // Mark this region as processed. This is needed for termination in case
1126 // a region is referenced more than once.
1127 Marked.insert(R);
1128
1129 // Mark the symbol for any live SymbolicRegion as "live". This means we
1130 // should continue to track that symbol.
1131 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek241677a2009-01-21 22:26:05 +00001132 SymReaper.markLive(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001133
1134 // Get the data binding for R (if any).
1135 RegionBindingsTy::data_type* Xptr = B.lookup(R);
1136 if (Xptr) {
1137 SVal X = *Xptr;
Ted Kremenek241677a2009-01-21 22:26:05 +00001138 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001139
1140 // If X is a region, then add it the RegionRoots.
1141 if (loc::MemRegionVal* RegionX = dyn_cast<loc::MemRegionVal>(&X))
1142 RegionRoots.push_back(RegionX->getRegion());
1143 }
1144
1145 // Get the subregions of R. These are RegionRoots as well since they
1146 // represent values that are also bound to R.
1147 const SubRegionsTy* SRptr = SubRegMap.lookup(R);
1148 if (!SRptr) continue;
1149 SubRegionsTy SR = *SRptr;
1150
1151 for (SubRegionsTy::iterator I=SR.begin(), E=SR.end(); I!=E; ++I)
1152 RegionRoots.push_back(*I);
1153 }
1154
1155 // We have now scanned the store, marking reachable regions and symbols
1156 // as live. We now remove all the regions that are dead from the store
Ted Kremenek3de2d3c2009-03-05 04:50:08 +00001157 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001158 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1159 const MemRegion* R = I.getKey();
1160
1161 // If this region live? Is so, none of its symbols are dead.
1162 if (Marked.count(R))
1163 continue;
1164
1165 // Remove this dead region from the store.
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001166 store = Remove(store, Loc::MakeVal(R));
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001167
1168 // Mark all non-live symbols that this region references as dead.
Ted Kremenek241677a2009-01-21 22:26:05 +00001169 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1170 SymReaper.maybeDead(SymR->getSymbol());
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001171
1172 SVal X = I.getData();
1173 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek241677a2009-01-21 22:26:05 +00001174 for (; SI != SE; ++SI) SymReaper.maybeDead(*SI);
Ted Kremenekc48ea6e2008-12-04 02:08:27 +00001175 }
1176
Zhongxing Xu8916d5b2008-11-10 09:39:04 +00001177 return store;
1178}
1179
Zhongxing Xua071eb02008-10-24 06:01:33 +00001180void RegionStoreManager::print(Store store, std::ostream& Out,
1181 const char* nl, const char *sep) {
1182 llvm::raw_os_ostream OS(Out);
1183 RegionBindingsTy B = GetRegionBindings(store);
1184 OS << "Store:" << nl;
1185
1186 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1187 OS << ' '; I.getKey()->print(OS); OS << " : ";
1188 I.getData().print(OS); OS << nl;
1189 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +00001190}
Zhongxing Xua82512a2008-10-24 08:42:28 +00001191
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001192const GRState* RegionStoreManager::BindArray(const GRState* St,
1193 const TypedRegion* R, SVal Init) {
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001194
1195 // FIXME: Verify we should use getLValueType or getRValueType.
Zhongxing Xu2ef93722008-12-14 03:14:52 +00001196 QualType T = R->getRValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001197 assert(T->isArrayType());
1198
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001199 // When we are binding the whole array, it always has default value 0.
1200 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001201 St = state.set<RegionDefaultValue>(R, NonLoc::MakeIntVal(getBasicVals(), 0,
1202 false));
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001203
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001204 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1205
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001206 llvm::APSInt Size(CAT->getSize(), false);
Sebastian Redl1be3da52009-01-26 19:54:12 +00001207 llvm::APSInt i = getBasicVals().getValue(0, Size.getBitWidth(),
1208 Size.isUnsigned());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001209
1210 // Check if the init expr is a StringLiteral.
1211 if (isa<loc::MemRegionVal>(Init)) {
1212 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1213 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1214 const char* str = S->getStrData();
1215 unsigned len = S->getByteLength();
1216 unsigned j = 0;
1217
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001218 // Copy bytes from the string literal into the target array. Trailing bytes
1219 // in the array that are not covered by the string literal are initialized
1220 // to zero.
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001221 for (; i < Size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001222 if (j >= len)
1223 break;
1224
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001225 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001226 ElementRegion* ER =
1227 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1228 Idx, R);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001229
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001230 SVal V = NonLoc::MakeVal(getBasicVals(), str[j], sizeof(char)*8, true);
1231 St = Bind(St, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001232 }
1233
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001234 return St;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001235 }
1236
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001237 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001238 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
1239
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001240 for (; i < Size; ++i, ++VI) {
1241 // The init list might be shorter than the array decl.
1242 if (VI == VE)
1243 break;
1244
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001245 SVal Idx = NonLoc::MakeVal(getBasicVals(), i);
Ted Kremenekf936f452009-05-04 06:18:28 +00001246 ElementRegion* ER =
1247 MRMgr.getElementRegion(cast<ArrayType>(T)->getElementType(),
1248 Idx, R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001249
1250 if (CAT->getElementType()->isStructureType())
1251 St = BindStruct(St, ER, *VI);
1252 else
1253 St = Bind(St, Loc::MakeVal(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001254 }
1255
Zhongxing Xubd4f7452009-03-09 06:49:50 +00001256 return St;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001257}
1258
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001259const GRState*
1260RegionStoreManager::BindStruct(const GRState* St, const TypedRegion* R, SVal V){
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001261 // FIXME: Verify that we should use getRValueType or getLValueType.
1262 QualType T = R->getRValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001263 assert(T->isStructureType());
1264
Zhongxing Xub13ecdb2009-03-12 03:45:35 +00001265 const RecordType* RT = T->getAsRecordType();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001266 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001267
1268 if (!RD->isDefinition())
1269 return St;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001270
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001271 if (V.isUnknown())
1272 return KillStruct(St, R);
1273
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001274 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001275 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001276 RecordDecl::field_iterator FI = RD->field_begin(getContext()),
1277 FE = RD->field_end(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001278
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001279 for (; FI != FE; ++FI, ++VI) {
1280
1281 // There may be fewer values than fields only when we are initializing a
1282 // struct decl. In this case, mark the region as having default value.
1283 if (VI == VE) {
Zhongxing Xu13020162008-12-24 07:29:24 +00001284 GRStateRef state(St, StateMgr);
Ted Kremenek14553ab2009-01-30 00:08:43 +00001285 const NonLoc& Idx = NonLoc::MakeIntVal(getBasicVals(), 0, false);
1286 St = state.set<RegionDefaultValue>(R, Idx);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001287 break;
1288 }
1289
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001290 QualType FTy = (*FI)->getType();
1291 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1292
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001293 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
1294 St = Bind(St, Loc::MakeVal(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001295
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001296 else if (FTy->isArrayType())
1297 St = BindArray(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001298
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001299 else if (FTy->isStructureType())
1300 St = BindStruct(St, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001301 }
1302
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001303 return St;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001304}
1305
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001306const GRState* RegionStoreManager::KillStruct(const GRState* St,
1307 const TypedRegion* R){
1308 GRStateRef state(St, StateMgr);
1309
1310 // Kill the struct region because it is assigned "unknown".
1311 St = state.add<RegionKills>(R);
1312
1313 // Set the default value of the struct region to "unknown".
1314 St = state.set<RegionDefaultValue>(R, UnknownVal());
1315
1316 Store store = St->getStore();
1317 RegionBindingsTy B = GetRegionBindings(store);
1318
1319 // Remove all bindings for the subregions of the struct.
1320 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1321 const MemRegion* r = I.getKey();
1322 if (const SubRegion* sr = dyn_cast<SubRegion>(r))
1323 if (sr->isSubRegionOf(R))
1324 store = Remove(store, Loc::MakeVal(sr));
Zhongxing Xu9c2a3db2009-01-13 03:07:41 +00001325 // FIXME: Maybe we should also remove the bindings for the "views" of the
1326 // subregions.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001327 }
1328
1329 return StateMgr.MakeStateWithStore(St, store);
1330}
1331
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001332const GRState* RegionStoreManager::AddRegionView(const GRState* St,
1333 const MemRegion* View,
1334 const MemRegion* Base) {
1335 GRStateRef state(St, StateMgr);
1336
1337 // First, retrieve the region view of the base region.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001338 const RegionViews* d = state.get<RegionViewMap>(Base);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001339 RegionViews L = d ? *d : RVFactory.GetEmptySet();
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001340
1341 // Now add View to the region view.
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001342 L = RVFactory.Add(L, View);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001343
1344 // Create a new state with the new region view.
Ted Kremenek50dc1b32008-12-24 01:05:03 +00001345 return state.set<RegionViewMap>(Base, L);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +00001346}
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001347
1348const GRState* RegionStoreManager::RemoveRegionView(const GRState* St,
1349 const MemRegion* View,
1350 const MemRegion* Base) {
1351 GRStateRef state(St, StateMgr);
1352
1353 // Retrieve the region view of the base region.
1354 const RegionViews* d = state.get<RegionViewMap>(Base);
1355
1356 // If the base region has no view, return.
1357 if (!d)
1358 return St;
1359
1360 // Remove the view.
1361 RegionViews V = *d;
1362 V = RVFactory.Remove(V, View);
1363
1364 return state.set<RegionViewMap>(Base, V);
1365}
Zhongxing Xu20794942009-05-06 08:33:50 +00001366
1367const GRState* RegionStoreManager::setCastType(const GRState* St,
1368 const MemRegion* R, QualType T) {
1369 GRStateRef state(St, StateMgr);
1370 return state.set<RegionCasts>(R, T);
1371}