blob: 13768eca8ca026fb77ad2156b803c8356fc8cdae [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
Zhongxing Xu0b143312009-08-21 13:25:15 +000018#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000019#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000020#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000024
25#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000027#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000028#include "llvm/Support/Compiler.h"
29
30using namespace clang;
31
Ted Kremenek356e9d62009-07-22 04:35:42 +000032#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000033#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000034
Zhongxing Xubaf03a72008-11-24 09:44:56 +000035// Actual Store type.
Ted Kremenek451ac092009-08-06 04:50:20 +000036typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000037
Ted Kremenek50dc1b32008-12-24 01:05:03 +000038//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000039// Fine-grained control of RegionStoreManager.
40//===----------------------------------------------------------------------===//
41
42namespace {
43struct VISIBILITY_HIDDEN minimal_features_tag {};
44struct VISIBILITY_HIDDEN maximal_features_tag {};
45
46class VISIBILITY_HIDDEN RegionStoreFeatures {
47 bool SupportsFields;
48 bool SupportsRemaining;
49
50public:
51 RegionStoreFeatures(minimal_features_tag) :
52 SupportsFields(false), SupportsRemaining(false) {}
53
54 RegionStoreFeatures(maximal_features_tag) :
55 SupportsFields(true), SupportsRemaining(false) {}
56
57 void enableFields(bool t) { SupportsFields = t; }
58
59 bool supportsFields() const { return SupportsFields; }
60 bool supportsRemaining() const { return SupportsRemaining; }
61};
62}
63
64//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000065// Region "Extents"
66//===----------------------------------------------------------------------===//
67//
68// MemRegions represent chunks of memory with a size (their "extent"). This
69// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000070//
Ted Kremenek50dc1b32008-12-24 01:05:03 +000071namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
72static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000073namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000074 template<> struct GRStateTrait<RegionExtents>
75 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
76 static void* GDMIndex() { return &RegionExtentsIndex; }
77 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +000078}
79
Ted Kremenek50dc1b32008-12-24 01:05:03 +000080//===----------------------------------------------------------------------===//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000081// Regions with default values.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000082//===----------------------------------------------------------------------===//
83//
Zhongxing Xu5834ed62009-01-13 01:49:57 +000084// This GDM entry tracks what regions have a default value if they have no bound
85// value and have not been killed.
Ted Kremenek50dc1b32008-12-24 01:05:03 +000086//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000087namespace {
88class VISIBILITY_HIDDEN RegionDefaultValue {
89public:
90 typedef llvm::ImmutableMap<const MemRegion*, SVal> MapTy;
91};
92}
Ted Kremenek50dc1b32008-12-24 01:05:03 +000093static int RegionDefaultValueIndex = 0;
94namespace clang {
95 template<> struct GRStateTrait<RegionDefaultValue>
Ted Kremenek19e1f0b2009-08-01 06:17:29 +000096 : public GRStatePartialTrait<RegionDefaultValue::MapTy> {
Ted Kremenek50dc1b32008-12-24 01:05:03 +000097 static void* GDMIndex() { return &RegionDefaultValueIndex; }
98 };
99}
100
Ted Kremenek451ac092009-08-06 04:50:20 +0000101typedef RegionDefaultValue::MapTy RegionDefaultBindings;
102
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000103//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000104// Utility functions.
105//===----------------------------------------------------------------------===//
106
107static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
108 if (ty->isAnyPointerType())
109 return true;
110
111 return ty->isIntegerType() && ty->isScalarType() &&
112 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
113}
114
115//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000116// Main RegionStore logic.
117//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000118
Zhongxing Xu17892752008-10-08 02:50:44 +0000119namespace {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000120
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
122 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
123 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
124 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000125 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000126public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000127 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000128 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000129
130 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000131 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000132 return true;
133 }
134
135 I->second = F.Add(I->second, SubRegion);
136 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000137 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000138
139 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000140
141 ~RegionStoreSubRegionMap() {}
142
Ted Kremenek5dc27462009-03-03 02:51:43 +0000143 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000144 Map::iterator I = M.find(Parent);
145
146 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000147 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148
149 llvm::ImmutableSet<const MemRegion*> S = I->second;
150 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
151 SI != SE; ++SI) {
152 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000153 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000154 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000155
156 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000158
159 typedef SetTy::iterator iterator;
160
161 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
162 Map::iterator I = M.find(R);
163 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
164 return std::make_pair(S.begin(), S.end());
165 }
Ted Kremenek59e8f112009-03-03 01:35:36 +0000166};
167
Zhongxing Xu17892752008-10-08 02:50:44 +0000168class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000169 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000170 RegionBindings::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000171
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000172 const MemRegion* SelfRegion;
Zhongxing Xu17892752008-10-08 02:50:44 +0000173
174public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000175 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000176 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000177 Features(f),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000178 RBFactory(mgr.getAllocator()),
Zhongxing Xu0b143312009-08-21 13:25:15 +0000179 SelfRegion(0) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000180
181 virtual ~RegionStoreManager() {}
182
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000183 SubRegionMap *getSubRegionMap(const GRState *state);
184
185 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000186
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000187
188 /// getDefaultBinding - Returns an SVal* representing an optional default
189 /// binding associated with a region and its subregions.
190 Optional<SVal> getDefaultBinding(const GRState *state, const MemRegion *R);
191
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000192 /// getLValueString - Returns an SVal representing the lvalue of a
193 /// StringLiteral. Within RegionStore a StringLiteral has an
194 /// associated StringRegion, and the lvalue of a StringLiteral is
195 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000196 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000197
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000198 /// getLValueCompoundLiteral - Returns an SVal representing the
199 /// lvalue of a compound literal. Within RegionStore a compound
200 /// literal has an associated region, and the lvalue of the
201 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000202 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000203
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000204 /// getLValueVar - Returns an SVal that represents the lvalue of a
205 /// variable. Within RegionStore a variable has an associated
206 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000207 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
208 const LocationContext *LC);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000209
Ted Kremenek67f28532009-06-17 22:02:04 +0000210 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000211
Ted Kremenek67f28532009-06-17 22:02:04 +0000212 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000213
Ted Kremenek67f28532009-06-17 22:02:04 +0000214 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000215
Ted Kremenek67f28532009-06-17 22:02:04 +0000216 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000217 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000218
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000219
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000220 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
221 /// type. 'Array' represents the lvalue of the array being decayed
222 /// to a pointer, and the returned SVal represents the decayed
223 /// version of that lvalue (i.e., a pointer to the first element of
224 /// the array). This is called by GRExprEngine when evaluating
225 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000226 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000227
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000228 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000229 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000230
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000231 Store getInitialStore(const LocationContext *InitLoc) {
Zhongxing Xu0b143312009-08-21 13:25:15 +0000232 RegionBindings B = RBFactory.GetEmptyMap();
233
234 // Eagerly bind 'self' to SelfRegion, because this is the only place we can
235 // get the ObjCMethodDecl.
236 typedef LiveVariables::AnalysisDataTy LVDataTy;
237 LVDataTy &D = InitLoc->getLiveVariables()->getAnalysisData();
238 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I!=E; ++I){
239 const NamedDecl *ND = I->first;
240
241 if (const ImplicitParamDecl *PD = dyn_cast<ImplicitParamDecl>(ND)) {
242 const Decl &CD = *InitLoc->getDecl();
243 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
244 if (MD->getSelfDecl() == PD) {
245 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
246 MRMgr.getHeapRegion());
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000247 B = RBFactory.Add(B, MRMgr.getVarRegion(PD, InitLoc),
Zhongxing Xu0b143312009-08-21 13:25:15 +0000248 ValMgr.makeLoc(SelfRegion));
249 }
250 }
251 }
252 }
253
254 return B.getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000255 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000256
257 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
258 /// 'this' object (C++). When used when analyzing a normal function this
259 /// method returns NULL.
260 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000261 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000262 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000263
264 //===-------------------------------------------------------------------===//
265 // Binding values to regions.
266 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000267
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000268 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
269 const Expr *E, unsigned Count);
270
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000271private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000272 void RemoveSubRegionBindings(RegionBindings &B,
273 RegionDefaultBindings &DVM,
274 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000275 const MemRegion *R,
276 RegionStoreSubRegionMap &M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000277
278public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000279 const GRState *Bind(const GRState *state, Loc LV, SVal V);
280
281 const GRState *BindCompoundLiteral(const GRState *state,
282 const CompoundLiteralExpr* CL, SVal V);
283
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000284 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
285 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000286
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000287 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
288 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000289 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000290 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000291
Ted Kremenek67f28532009-06-17 22:02:04 +0000292 /// BindStruct - Bind a compound value to a structure.
293 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
294
295 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
296
297 /// KillStruct - Set the entire struct to unknown.
298 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
299
300 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
301
302 Store Remove(Store store, Loc LV);
303
304 //===------------------------------------------------------------------===//
305 // Loading values from regions.
306 //===------------------------------------------------------------------===//
307
308 /// The high level logic for this method is this:
309 /// Retrieve (L)
310 /// if L has binding
311 /// return L's binding
312 /// else if L is in killset
313 /// return unknown
314 /// else
315 /// if L is on stack or heap
316 /// return undefined
317 /// else
318 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000319 SValuator::CastResult Retrieve(const GRState *state, Loc L,
320 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000321
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000322 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000323
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000324 SVal RetrieveField(const GRState *state, const FieldRegion *R);
325
326 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000327
Ted Kremenek9031dd72009-07-21 00:12:07 +0000328 SVal RetrieveVar(const GRState *state, const VarRegion *R);
329
Ted Kremenek25c54572009-07-20 22:58:02 +0000330 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
331
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000332 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
333 QualType Ty, const MemRegion *superR);
334
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000335 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
336 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000337
Ted Kremenek67f28532009-06-17 22:02:04 +0000338 /// Retrieve the values in a struct and return a CompoundVal, used when doing
339 /// struct copy:
340 /// struct s x, y;
341 /// x = y;
342 /// y's value is retrieved by this method.
343 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
344
345 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000346
347 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000348 GetLazyBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000349
350 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
351 const GRState *state,
352 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000353
354 //===------------------------------------------------------------------===//
355 // State pruning.
356 //===------------------------------------------------------------------===//
357
358 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
359 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000360 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000361 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
362
363 //===------------------------------------------------------------------===//
364 // Region "extents".
365 //===------------------------------------------------------------------===//
366
367 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
368 SVal getSizeInElements(const GRState *state, const MemRegion* R);
369
370 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000371 // Utility methods.
372 //===------------------------------------------------------------------===//
373
Ted Kremenek451ac092009-08-06 04:50:20 +0000374 static inline RegionBindings GetRegionBindings(Store store) {
375 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000376 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000377
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000378 void print(Store store, llvm::raw_ostream& Out, const char* nl,
379 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000380
381 void iterBindings(Store store, BindingsHandler& f) {
382 // FIXME: Implement.
383 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000384
Ted Kremenek67f28532009-06-17 22:02:04 +0000385 // FIXME: Remove.
386 BasicValueFactory& getBasicVals() {
387 return StateMgr.getBasicVals();
388 }
389
390 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000391 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000392};
393
394} // end anonymous namespace
395
Ted Kremenek9af46f52009-06-16 22:36:44 +0000396//===----------------------------------------------------------------------===//
397// RegionStore creation.
398//===----------------------------------------------------------------------===//
399
400StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
401 RegionStoreFeatures F = maximal_features_tag();
402 return new RegionStoreManager(StMgr, F);
403}
404
405StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
406 RegionStoreFeatures F = minimal_features_tag();
407 F.enableFields(true);
408 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000409}
410
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000411void
412RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
413 const SubRegion *R) {
414 const MemRegion *superR = R->getSuperRegion();
415 if (add(superR, R))
416 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
417 WL.push_back(sr);
418}
419
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000420RegionStoreSubRegionMap*
421RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000422 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000423 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
424
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000425 llvm::SmallVector<const SubRegion*, 10> WL;
426
Ted Kremenek451ac092009-08-06 04:50:20 +0000427 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
429 M->process(WL, R);
430
Ted Kremenek451ac092009-08-06 04:50:20 +0000431 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
432 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000433 I != E; ++I)
434 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
435 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000436
437 // We also need to record in the subregion map "intermediate" regions that
438 // don't have direct bindings but are super regions of those that do.
439 while (!WL.empty()) {
440 const SubRegion *R = WL.back();
441 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000442 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000443 }
444
Ted Kremenek14453bf2009-03-03 19:02:42 +0000445 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000446}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000447
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000448SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
449 return getRegionStoreSubRegionMap(state);
450}
451
Ted Kremenek9af46f52009-06-16 22:36:44 +0000452//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000453// Binding invalidation.
454//===----------------------------------------------------------------------===//
455
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000456void
Ted Kremenek451ac092009-08-06 04:50:20 +0000457RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
458 RegionDefaultBindings &DVM,
459 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000460 const MemRegion *R,
461 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000462
463 RegionStoreSubRegionMap::iterator I, E;
464
465 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000466 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000467
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000468 B = RBFactory.Remove(B, R);
469 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000470}
471
472
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000473const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
474 const MemRegion *R,
475 const Expr *E,
476 unsigned Count) {
477 ASTContext& Ctx = StateMgr.getContext();
478
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000479 // Strip away casts.
480 R = R->getBaseRegion();
481
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000482 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000483 {
484 // Get the mapping of regions -> subregions.
485 llvm::OwningPtr<RegionStoreSubRegionMap>
486 SubRegions(getRegionStoreSubRegionMap(state));
487
Ted Kremenek451ac092009-08-06 04:50:20 +0000488 RegionBindings B = GetRegionBindings(state->getStore());
489 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
490 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000491 state->get_context<RegionDefaultValue>();
492
493 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
494 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
495 }
496
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000497 if (!R->isBoundable())
498 return state;
499
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000500 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
501 isa<ObjCObjectRegion>(R)) {
502 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000503 // conjured symbol. The type of the symbol is irrelavant.
504 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000505 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000506 }
507
508 const TypedRegion *TR = cast<TypedRegion>(R);
509 QualType T = TR->getValueType(Ctx);
510
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000511 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000512 // FIXME: handle structs with default region value.
513 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
514
515 // No record definition. There is nothing we can do.
516 if (!RD)
517 return state;
518
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000519 // Invalidate the region by setting its default value to
520 // conjured symbol. The type of the symbol is irrelavant.
521 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
522 return setDefaultValue(state, R, V);
523 }
524
525 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000526 // Set the default value of the array to conjured symbol.
527 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
528 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000529 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000530 }
531
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000532 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
533 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
534 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000535}
536
537//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000538// getLValueXXX methods.
539//===----------------------------------------------------------------------===//
540
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000541/// getLValueString - Returns an SVal representing the lvalue of a
542/// StringLiteral. Within RegionStore a StringLiteral has an
543/// associated StringRegion, and the lvalue of a StringLiteral is the
544/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000545SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000546 const StringLiteral* S) {
547 return loc::MemRegionVal(MRMgr.getStringRegion(S));
548}
549
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000550/// getLValueVar - Returns an SVal that represents the lvalue of a
551/// variable. Within RegionStore a variable has an associated
552/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000553SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
554 const LocationContext *LC) {
555 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000556}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000557
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000558/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
559/// of a compound literal. Within RegionStore a compound literal
560/// has an associated region, and the lvalue of the compound literal
561/// is the lvalue of that region.
562SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000563RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000564 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000565 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
566}
567
Ted Kremenek67f28532009-06-17 22:02:04 +0000568SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000569 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000570 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000571}
572
Ted Kremenek67f28532009-06-17 22:02:04 +0000573SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000574 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000575 return getLValueFieldOrIvar(St, Base, D);
576}
577
Ted Kremenek67f28532009-06-17 22:02:04 +0000578SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000579 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000580 if (Base.isUnknownOrUndef())
581 return Base;
582
583 Loc BaseL = cast<Loc>(Base);
584 const MemRegion* BaseR = 0;
585
586 switch (BaseL.getSubKind()) {
587 case loc::MemRegionKind:
588 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
589 break;
590
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000591 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592 // These are anormal cases. Flag an undefined value.
593 return UndefinedVal();
594
595 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000596 // While these seem funny, this can happen through casts.
597 // FIXME: What we should return is the field offset. For example,
598 // add the field offset to the integer value. That way funny things
599 // like this work properly: &(((struct foo *) 0xa)->f)
600 return Base;
601
602 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000603 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000604 return Base;
605 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000606
607 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
608 // of FieldDecl.
609 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
610 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000611
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000612 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000613}
614
Ted Kremenek67f28532009-06-17 22:02:04 +0000615SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000616 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000617 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000618
Ted Kremenekde7ec632009-03-09 22:44:49 +0000619 // If the base is an unknown or undefined value, just return it back.
620 // FIXME: For absolute pointer addresses, we just return that value back as
621 // well, although in reality we should return the offset added to that
622 // value.
623 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000624 return Base;
625
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000626 // Only handle integer offsets... for now.
627 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000628 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000629
Zhongxing Xuce760782009-05-09 13:20:07 +0000630 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000631
632 // Pointer of any type can be cast and used as array base.
633 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
634
Ted Kremenek46537392009-07-16 01:33:37 +0000635 // Convert the offset to the appropriate size and signedness.
636 Offset = ValMgr.convertToArrayIndex(Offset);
637
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000638 if (!ElemR) {
639 //
640 // If the base region is not an ElementRegion, create one.
641 // This can happen in the following example:
642 //
643 // char *p = __builtin_alloc(10);
644 // p[1] = 8;
645 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000646 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000647 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000648 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000649 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000650 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000651
652 SVal BaseIdx = ElemR->getIndex();
653
654 if (!isa<nonloc::ConcreteInt>(BaseIdx))
655 return UnknownVal();
656
657 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
658 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
659 assert(BaseIdxI.isSigned());
660
Ted Kremenek46537392009-07-16 01:33:37 +0000661 // Compute the new index.
662 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000663
Ted Kremenek46537392009-07-16 01:33:37 +0000664 // Construct the new ElementRegion.
665 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000666 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
667 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000668}
669
Ted Kremenek9af46f52009-06-16 22:36:44 +0000670//===----------------------------------------------------------------------===//
671// Extents for regions.
672//===----------------------------------------------------------------------===//
673
Ted Kremenek67f28532009-06-17 22:02:04 +0000674SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000675 const MemRegion *R) {
676
677 switch (R->getKind()) {
678 case MemRegion::MemSpaceRegionKind:
679 assert(0 && "Cannot index into a MemSpace");
680 return UnknownVal();
681
682 case MemRegion::CodeTextRegionKind:
683 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000684 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000685
686 // Not yet handled.
687 case MemRegion::AllocaRegionKind:
688 case MemRegion::CompoundLiteralRegionKind:
689 case MemRegion::ElementRegionKind:
690 case MemRegion::FieldRegionKind:
691 case MemRegion::ObjCIvarRegionKind:
692 case MemRegion::ObjCObjectRegionKind:
693 case MemRegion::SymbolicRegionKind:
694 return UnknownVal();
695
696 case MemRegion::StringRegionKind: {
697 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
698 // We intentionally made the size value signed because it participates in
699 // operations with signed indices.
700 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000701 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000702
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703 case MemRegion::VarRegionKind: {
704 const VarRegion* VR = cast<VarRegion>(R);
705 // Get the type of the variable.
706 QualType T = VR->getDesugaredValueType(getContext());
707
708 // FIXME: Handle variable-length arrays.
709 if (isa<VariableArrayType>(T))
710 return UnknownVal();
711
712 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
713 // return the size as signed integer.
714 return ValMgr.makeIntVal(CAT->getSize(), false);
715 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000716
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717 // Clients can use ordinary variables as if they were arrays. These
718 // essentially are arrays of size 1.
719 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000720 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000721
722 case MemRegion::BEG_DECL_REGIONS:
723 case MemRegion::END_DECL_REGIONS:
724 case MemRegion::BEG_TYPED_REGIONS:
725 case MemRegion::END_TYPED_REGIONS:
726 assert(0 && "Infeasible region");
727 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000728 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000729
730 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000731 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000732}
733
Ted Kremenek67f28532009-06-17 22:02:04 +0000734const GRState *RegionStoreManager::setExtent(const GRState *state,
735 const MemRegion *region,
736 SVal extent) {
737 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000738}
739
740//===----------------------------------------------------------------------===//
741// Location and region casting.
742//===----------------------------------------------------------------------===//
743
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000744/// ArrayToPointer - Emulates the "decay" of an array to a pointer
745/// type. 'Array' represents the lvalue of the array being decayed
746/// to a pointer, and the returned SVal represents the decayed
747/// version of that lvalue (i.e., a pointer to the first element of
748/// the array). This is called by GRExprEngine when evaluating casts
749/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000750SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000751 if (!isa<loc::MemRegionVal>(Array))
752 return UnknownVal();
753
754 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
755 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
756
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000757 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000758 return UnknownVal();
759
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000760 // Strip off typedefs from the ArrayRegion's ValueType.
761 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000762 ArrayType *AT = cast<ArrayType>(T);
763 T = AT->getElementType();
764
Ted Kremenek75185b52009-07-16 00:00:11 +0000765 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
766 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000767
768 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000769}
770
Ted Kremenek9af46f52009-06-16 22:36:44 +0000771//===----------------------------------------------------------------------===//
772// Pointer arithmetic.
773//===----------------------------------------------------------------------===//
774
Zhongxing Xu262fd032009-05-20 09:00:16 +0000775SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000776 BinaryOperator::Opcode Op, Loc L, NonLoc R,
777 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000778 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000779 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000780 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000781
Zhongxing Xua1718c72009-04-03 07:33:13 +0000782 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000783 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000784
Ted Kremenek3bccf082009-07-11 00:58:27 +0000785 switch (MR->getKind()) {
786 case MemRegion::SymbolicRegionKind: {
787 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000788 SymbolRef Sym = SR->getSymbol();
789 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000790 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000791 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
792 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
793 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000794 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000795 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000796 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000797 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000798 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000799 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
800 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
801 break;
802 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000803
Ted Kremenek3bccf082009-07-11 00:58:27 +0000804 case MemRegion::ElementRegionKind: {
805 ER = cast<ElementRegion>(MR);
806 break;
807 }
808
809 // Not yet handled.
810 case MemRegion::VarRegionKind:
811 case MemRegion::StringRegionKind:
812 case MemRegion::CompoundLiteralRegionKind:
813 case MemRegion::FieldRegionKind:
814 case MemRegion::ObjCObjectRegionKind:
815 case MemRegion::ObjCIvarRegionKind:
816 return UnknownVal();
817
Ted Kremenek3bccf082009-07-11 00:58:27 +0000818 case MemRegion::CodeTextRegionKind:
819 // Technically this can happen if people do funny things with casts.
820 return UnknownVal();
821
822 case MemRegion::MemSpaceRegionKind:
823 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
824 return UnknownVal();
825
826 case MemRegion::BEG_DECL_REGIONS:
827 case MemRegion::END_DECL_REGIONS:
828 case MemRegion::BEG_TYPED_REGIONS:
829 case MemRegion::END_TYPED_REGIONS:
830 assert(0 && "Infeasible region");
831 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000832 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000833
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000834 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000835 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
836 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
837
838 // Only support concrete integer indexes for now.
839 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000840 // FIXME: Should use SValuator here.
841 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
842 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000843 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000844 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
845 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000846 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000847 }
848
849 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000850}
851
Ted Kremenek9af46f52009-06-16 22:36:44 +0000852//===----------------------------------------------------------------------===//
853// Loading values from regions.
854//===----------------------------------------------------------------------===//
855
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000856Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
857 const MemRegion *R) {
858
859 if (R->isBoundable())
860 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
861 if (TR->getValueType(getContext())->isUnionType())
862 return UnknownVal();
863
864 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
865}
866
Ted Kremeneka6275a52009-07-15 02:31:43 +0000867static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
868 RTy = Ctx.getCanonicalType(RTy);
869 UsedTy = Ctx.getCanonicalType(UsedTy);
870
871 if (RTy == UsedTy)
872 return false;
873
Ted Kremenek25c54572009-07-20 22:58:02 +0000874
875 // Recursively check the types. We basically want to see if a pointer value
876 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
877 // represents a reinterpretation.
878 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000879 const PointerType *PRTy = RTy->getAs<PointerType>();
880 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000881
882 return PUsedTy && PRTy &&
883 IsReinterpreted(PRTy->getPointeeType(),
884 PUsedTy->getPointeeType(), Ctx);
885 }
886
887 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000888}
889
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000890SValuator::CastResult
891RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000892
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000893 assert(!isa<UnknownVal>(L) && "location unknown");
894 assert(!isa<UndefinedVal>(L) && "location undefined");
895
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000896 // FIXME: Is this even possible? Shouldn't this be treated as a null
897 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000898 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000899 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000900
Ted Kremenek67f28532009-06-17 22:02:04 +0000901 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000902
Zhongxing Xu91844122009-05-20 09:18:48 +0000903 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000904 // Example:
905 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000906 // char* p = alloca();
907 // read(p);
908 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000909 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000910 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000911
912 if (isa<SymbolicRegion>(MR)) {
913 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000914 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000915 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000916 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
917 }
918
Ted Kremenek968f0a62009-08-03 21:41:46 +0000919 if (isa<CodeTextRegion>(MR))
920 return SValuator::CastResult(state, UnknownVal());
921
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000922 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
923 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000924 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000925 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000926
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000927 // FIXME: We should eventually handle funny addressing. e.g.:
928 //
929 // int x = ...;
930 // int *p = &x;
931 // char *q = (char*) p;
932 // char c = *q; // returns the first byte of 'x'.
933 //
934 // Such funny addressing will occur due to layering of regions.
935
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000936#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000937 ASTContext &Ctx = getContext();
938 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000939 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
940 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000941 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000942 assert(Ctx.getCanonicalType(RTy) ==
943 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000944 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000945#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000946
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000947 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000948 return SValuator::CastResult(state, RetrieveStruct(state, R));
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000949
950 // FIXME: Handle unions.
951 if (RTy->isUnionType())
952 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000953
954 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000955 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000956
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000957 // FIXME: handle Vector types.
958 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000959 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000960
961 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000962 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000963
964 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000965 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000966
Ted Kremenek25c54572009-07-20 22:58:02 +0000967 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000968 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000969
970 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000971 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000972
Ted Kremenek451ac092009-08-06 04:50:20 +0000973 RegionBindings B = GetRegionBindings(state->getStore());
974 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000975
976 // Check if the region has a binding.
977 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000978 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000979
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000980 // The location does not have a bound value. This means that it has
981 // the value it had upon its creation and/or entry to the analyzed
982 // function/method. These are either symbolic values or 'undefined'.
983
Ted Kremenek356e9d62009-07-22 04:35:42 +0000984#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000985 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000986#else
987 if (R->hasStackStorage()) {
988#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000989 // All stack variables are considered to have undefined values
990 // upon creation. All heap allocated blocks are considered to
991 // have undefined values as well unless they are explicitly bound
992 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000993 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000994 }
995
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000996 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000997 return SValuator::CastResult(state,
998 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000999}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001000
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001001std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001002RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001003
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001004 if (const nonloc::LazyCompoundVal *V =
1005 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
1006 return std::make_pair(V->getState(), V->getRegion());
1007
1008 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1009 const std::pair<const GRState *, const MemRegion *> &X =
1010 GetLazyBinding(B, ER->getSuperRegion());
1011
1012 if (X.first)
1013 return std::make_pair(X.first,
1014 MRMgr.getElementRegionWithSuper(ER, X.second));
1015 }
1016 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1017 const std::pair<const GRState *, const MemRegion *> &X =
1018 GetLazyBinding(B, FR->getSuperRegion());
1019
1020 if (X.first)
1021 return std::make_pair(X.first,
1022 MRMgr.getFieldRegionWithSuper(FR, X.second));
1023 }
1024
1025 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1026}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001027
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001028SVal RegionStoreManager::RetrieveElement(const GRState* state,
1029 const ElementRegion* R) {
1030 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001031 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001032 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001033 return *V;
1034
Ted Kremenek921109a2009-07-01 23:19:52 +00001035 const MemRegion* superR = R->getSuperRegion();
1036
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001037 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001038 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001039 const StringLiteral *Str = StrR->getStringLiteral();
1040 SVal Idx = R->getIndex();
1041 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1042 int64_t i = CI->getValue().getSExtValue();
1043 char c;
1044 if (i == Str->getByteLength())
1045 c = '\0';
1046 else
1047 c = Str->getStrData()[i];
1048 return ValMgr.makeIntVal(c, getContext().CharTy);
1049 }
1050 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001051
1052 // Special case: the current region represents a cast and it and the super
1053 // region both have pointer types or intptr_t types. If so, perform the
1054 // retrieve from the super region and appropriately "cast" the value.
1055 // This is needed to support OSAtomicCompareAndSwap and friends or other
1056 // loads that treat integers as pointers and vis versa.
1057 if (R->getIndex().isZeroConstant()) {
1058 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1059 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001060 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1061 QualType valTy = R->getValueType(Ctx);
1062 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1063 // Retrieve the value from the super region. This will be casted to
1064 // valTy when we return to 'Retrieve'.
1065 const SValuator::CastResult &cr = Retrieve(state,
1066 loc::MemRegionVal(superR),
1067 valTy);
1068 return cr.getSVal();
1069 }
1070 }
1071 }
1072 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001073
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001074 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001075 if (const SVal *V = B.lookup(superR)) {
1076 if (SymbolRef parentSym = V->getAsSymbol())
1077 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001078
1079 if (V->isUnknownOrUndef())
1080 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001081
1082 // Handle LazyCompoundVals for the immediate super region. Other cases
1083 // are handled in 'RetrieveFieldOrElementCommon'.
1084 if (const nonloc::LazyCompoundVal *LCV =
1085 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001086
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001087 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1088 return RetrieveElement(LCV->getState(), R);
1089 }
1090
Ted Kremeneka6275a52009-07-15 02:31:43 +00001091 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001092 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001093 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001094
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001095 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001096}
1097
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001098SVal RegionStoreManager::RetrieveField(const GRState* state,
1099 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001100
1101 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001102 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001103 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001104 return *V;
1105
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001106 QualType Ty = R->getValueType(getContext());
1107 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1108}
1109
1110SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1111 const TypedRegion *R,
1112 QualType Ty,
1113 const MemRegion *superR) {
1114
1115 // At this point we have already checked in either RetrieveElement or
1116 // RetrieveField if 'R' has a direct binding.
1117
1118 RegionBindings B = GetRegionBindings(state->getStore());
1119
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001120 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001121 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001122 if (SymbolRef parentSym = D->getAsSymbol())
1123 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001124
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001125 if (D->isZeroConstant())
1126 return ValMgr.makeZeroVal(Ty);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001127
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001128 if (D->isUnknown())
1129 return *D;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001130
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001131 assert(0 && "Unknown default value");
1132 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001133
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001134 // If our super region is a field or element itself, walk up the region
1135 // hierarchy to see if there is a default value installed in an ancestor.
1136 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1137 superR = cast<SubRegion>(superR)->getSuperRegion();
1138 continue;
1139 }
1140
1141 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001142 }
1143
1144 // Lazy binding?
1145 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001146 const MemRegion *lazyBindingRegion = NULL;
1147 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001148
1149 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001150 assert(lazyBindingRegion && "Lazy-binding region not set");
1151
1152 if (isa<ElementRegion>(R))
1153 return RetrieveElement(lazyBindingState,
1154 cast<ElementRegion>(lazyBindingRegion));
1155
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001156 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001157 cast<FieldRegion>(lazyBindingRegion));
1158 }
Ted Kremenekdc147262009-07-02 22:02:15 +00001159
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001160 if (R->hasStackStorage() && !R->hasParametersStorage()) {
1161
1162 if (isa<ElementRegion>(R)) {
1163 // Currently we don't reason specially about Clang-style vectors. Check
1164 // if superR is a vector and if so return Unknown.
1165 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1166 if (typedSuperR->getValueType(getContext())->isVectorType())
1167 return UnknownVal();
1168 }
1169 }
1170
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001171 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001172 }
1173
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001174 // All other values are symbolic.
1175 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001176}
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001177
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001178SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1179 const ObjCIvarRegion* R) {
1180
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001181 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001182 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001183
1184 if (const SVal* V = B.lookup(R))
1185 return *V;
1186
1187 const MemRegion *superR = R->getSuperRegion();
1188
1189 // Check if the super region has a binding.
1190 if (const SVal *V = B.lookup(superR)) {
1191 if (SymbolRef parentSym = V->getAsSymbol())
1192 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1193
1194 // Other cases: give up.
1195 return UnknownVal();
1196 }
1197
Ted Kremenek25c54572009-07-20 22:58:02 +00001198 return RetrieveLazySymbol(state, R);
1199}
1200
Ted Kremenek9031dd72009-07-21 00:12:07 +00001201SVal RegionStoreManager::RetrieveVar(const GRState *state,
1202 const VarRegion *R) {
1203
1204 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001205 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek9031dd72009-07-21 00:12:07 +00001206
1207 if (const SVal* V = B.lookup(R))
1208 return *V;
1209
1210 // Lazily derive a value for the VarRegion.
1211 const VarDecl *VD = R->getDecl();
1212
Ted Kremenek9031dd72009-07-21 00:12:07 +00001213 if (R->hasGlobalsOrParametersStorage())
1214 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1215
1216 return UndefinedVal();
1217}
1218
Ted Kremenek25c54572009-07-20 22:58:02 +00001219SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1220 const TypedRegion *R) {
1221
1222 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001223
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001224 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001225 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001226}
1227
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001228SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1229 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001230 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001231 assert(T->isStructureType());
1232
Zhongxing Xub7507d12009-06-11 07:27:30 +00001233 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001234 RecordDecl* RD = RT->getDecl();
1235 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001236 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001237#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001238 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1239
Ted Kremenek67f28532009-06-17 22:02:04 +00001240 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1241 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001242 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001243
Douglas Gregore267ff32008-12-11 20:41:00 +00001244 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1245 FieldEnd = Fields.rend();
1246 Field != FieldEnd; ++Field) {
1247 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001248 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001249 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001250 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1251 }
1252
Zhongxing Xud91ee272009-06-23 09:02:15 +00001253 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001254#else
1255 return ValMgr.makeLazyCompoundVal(state, R);
1256#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001257}
1258
Ted Kremenek67f28532009-06-17 22:02:04 +00001259SVal RegionStoreManager::RetrieveArray(const GRState *state,
1260 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001261#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001262 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001263 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1264
1265 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001266 uint64_t size = CAT->getSize().getZExtValue();
1267 for (uint64_t i = 0; i < size; ++i) {
1268 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001269 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1270 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001271 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001272 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001273 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1274 }
1275
Zhongxing Xud91ee272009-06-23 09:02:15 +00001276 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001277#else
1278 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1279 return ValMgr.makeLazyCompoundVal(state, R);
1280#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001281}
1282
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001283SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1284 const GRState *state,
1285 const TypedRegion *R,
1286 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001287 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001288 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001289
1290 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001291 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001292}
1293
Ted Kremenek9af46f52009-06-16 22:36:44 +00001294//===----------------------------------------------------------------------===//
1295// Binding values to regions.
1296//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001297
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001298Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001299 const MemRegion* R = 0;
1300
1301 if (isa<loc::MemRegionVal>(L))
1302 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001303
1304 if (R) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001305 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001306 return RBFactory.Remove(B, R).getRoot();
1307 }
1308
1309 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001310}
1311
Ted Kremenek67f28532009-06-17 22:02:04 +00001312const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001313 if (isa<loc::ConcreteInt>(L))
1314 return state;
1315
Ted Kremenek9af46f52009-06-16 22:36:44 +00001316 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001317 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001318
1319 // Check if the region is a struct region.
1320 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1321 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001322 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001323
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001324 // Special case: the current region represents a cast and it and the super
1325 // region both have pointer types or intptr_t types. If so, perform the
1326 // bind to the super region.
1327 // This is needed to support OSAtomicCompareAndSwap and friends or other
1328 // loads that treat integers as pointers and vis versa.
1329 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1330 if (ER->getIndex().isZeroConstant()) {
1331 if (const TypedRegion *superR =
1332 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1333 ASTContext &Ctx = getContext();
1334 QualType superTy = superR->getValueType(Ctx);
1335 QualType erTy = ER->getValueType(Ctx);
1336
1337 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1338 IsAnyPointerOrIntptr(erTy, Ctx)) {
1339 SValuator::CastResult cr =
1340 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1341 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1342 }
1343 }
1344 }
1345 }
1346
1347 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001348 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001349 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001350}
1351
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001352const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1353 const VarDecl *VD,
1354 const LocationContext *LC,
1355 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001356
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001357 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001358 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001359
Ted Kremenek0964a062009-01-21 06:57:53 +00001360 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001361 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001362 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001363 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001364
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001365 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001366}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001367
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001368// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001369const GRState *
1370RegionStoreManager::BindCompoundLiteral(const GRState *state,
1371 const CompoundLiteralExpr* CL,
1372 SVal V) {
1373
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001374 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001375 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001376}
1377
Ted Kremenek67f28532009-06-17 22:02:04 +00001378const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001379 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001380 SVal Init) {
1381
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001382 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001383 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001384 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001385
Ted Kremenek46537392009-07-16 01:33:37 +00001386 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001387
1388 // Check if the init expr is a StringLiteral.
1389 if (isa<loc::MemRegionVal>(Init)) {
1390 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1391 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1392 const char* str = S->getStrData();
1393 unsigned len = S->getByteLength();
1394 unsigned j = 0;
1395
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001396 // Copy bytes from the string literal into the target array. Trailing bytes
1397 // in the array that are not covered by the string literal are initialized
1398 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001399 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001400 if (j >= len)
1401 break;
1402
Ted Kremenek46537392009-07-16 01:33:37 +00001403 SVal Idx = ValMgr.makeArrayIndex(i);
1404 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1405 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001406
Zhongxing Xud91ee272009-06-23 09:02:15 +00001407 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001408 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001409 }
1410
Ted Kremenek67f28532009-06-17 22:02:04 +00001411 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001412 }
1413
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001414 // Handle lazy compound values.
1415 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1416 return CopyLazyBindings(*LCV, state, R);
1417
1418 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001419 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001420 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001421 uint64_t i = 0;
1422
1423 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001424 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001425 if (VI == VE)
1426 break;
1427
Ted Kremenek46537392009-07-16 01:33:37 +00001428 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001429 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001430
1431 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001432 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001433 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001434 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001435 }
1436
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001437 // If the init list is shorter than the array length, set the array default
1438 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001439 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001440 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001441 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001442 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001443 }
1444 }
1445
Ted Kremenek67f28532009-06-17 22:02:04 +00001446 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001447}
1448
Ted Kremenek67f28532009-06-17 22:02:04 +00001449const GRState *
1450RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1451 SVal V) {
1452
1453 if (!Features.supportsFields())
1454 return state;
1455
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001456 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001457 assert(T->isStructureType());
1458
Ted Kremenek6217b802009-07-29 21:53:49 +00001459 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001460 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001461
1462 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001463 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001464
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001465 // Handle lazy compound values.
1466 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1467 return CopyLazyBindings(*LCV, state, R);
1468
Ted Kremenek67f28532009-06-17 22:02:04 +00001469 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1470 // Ignore them and kill the field values.
1471 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1472 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001473
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001474 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001475 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001476
1477 RecordDecl::field_iterator FI, FE;
1478
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001479 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001480
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001481 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001482 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001483
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001484 QualType FTy = (*FI)->getType();
1485 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1486
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001487 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001488 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001489 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001490 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001491 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001492 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001493 }
1494
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001495 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001496 if (FI != FE)
1497 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001498
Ted Kremenek67f28532009-06-17 22:02:04 +00001499 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001500}
1501
Ted Kremenek67f28532009-06-17 22:02:04 +00001502const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001503 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001504
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001505 // Set the default value of the struct region to "unknown".
1506 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001507
1508 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001509 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001510 RegionBindings B = GetRegionBindings(store);
1511 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001512 const MemRegion* R = I.getKey();
1513 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1514 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001515 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001516 }
1517
Ted Kremenek67f28532009-06-17 22:02:04 +00001518 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001519}
1520
Ted Kremenek67f28532009-06-17 22:02:04 +00001521const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1522 const MemRegion* R, SVal V) {
1523 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001524}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001525
1526const GRState*
1527RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1528 const GRState *state,
1529 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001530
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001531 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001532 RegionBindings B = GetRegionBindings(state->getStore());
1533 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1534 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001535 state->get_context<RegionDefaultValue>();
1536
1537 llvm::OwningPtr<RegionStoreSubRegionMap>
1538 SubRegions(getRegionStoreSubRegionMap(state));
1539
1540 // B and DVM are updated after the call to RemoveSubRegionBindings.
1541 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1542
1543 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1544 // results in a zero-copy algorithm.
1545 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1546}
1547
Ted Kremenek9af46f52009-06-16 22:36:44 +00001548//===----------------------------------------------------------------------===//
1549// State pruning.
1550//===----------------------------------------------------------------------===//
Ted Kremenek451ac092009-08-06 04:50:20 +00001551
Ted Kremenek9af46f52009-06-16 22:36:44 +00001552static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1553 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1554 const MemRegion *R = XR->getRegion();
1555
1556 while (R) {
1557 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1558 SymReaper.markLive(SR->getSymbol());
1559 return;
1560 }
1561
1562 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1563 R = SR->getSuperRegion();
1564 continue;
1565 }
1566
1567 break;
1568 }
1569
1570 return;
1571 }
1572
1573 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1574 SymReaper.markLive(*SI);
1575}
Ted Kremenek451ac092009-08-06 04:50:20 +00001576
1577namespace {
1578class VISIBILITY_HIDDEN TreeScanner {
1579 RegionBindings B;
1580 RegionDefaultBindings DB;
1581 SymbolReaper &SymReaper;
1582 llvm::DenseSet<const MemRegion*> &Marked;
1583 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1584 RegionStoreSubRegionMap &M;
1585 RegionStoreManager &RS;
1586 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1587 const bool MarkKeys;
1588public:
1589 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1590 SymbolReaper &symReaper,
1591 llvm::DenseSet<const MemRegion*> &marked,
1592 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1593 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1594 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1595 bool markKeys = true)
1596 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1597 ScannedLazyVals(scannedLazyVals), M(m),
1598 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
1599
1600 void scanTree(const MemRegion *R);
1601};
1602} // end anonymous namespace
1603
1604
1605void TreeScanner::scanTree(const MemRegion *R) {
1606 if (MarkKeys) {
1607 if (Marked.count(R))
1608 return;
1609
1610 Marked.insert(R);
1611 }
1612
1613 // Mark the symbol for any live SymbolicRegion as "live". This means we
1614 // should continue to track that symbol.
1615 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1616 SymReaper.markLive(SymR->getSymbol());
1617
1618 // Get the data binding for R (if any).
1619 const SVal* Xptr = B.lookup(R);
1620
1621 // Check for lazy bindings.
1622 if (const nonloc::LazyCompoundVal *V =
1623 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1624
1625 const LazyCompoundValData *D = V->getCVData();
1626
1627 if (!ScannedLazyVals.count(D)) {
1628 // Scan the bindings in the LazyCompoundVal.
1629 ScannedLazyVals.insert(D);
1630
1631 // FIXME: Cache subregion maps.
1632 const GRState *lazyState = D->getState();
1633
1634 llvm::OwningPtr<RegionStoreSubRegionMap>
1635 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
1636
1637 Store lazyStore = lazyState->getStore();
1638 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
1639
1640 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
1641
1642 // Scan the bindings.
1643 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1644 *lazySM.get(), RS, RegionRoots, false);
1645
1646 scan.scanTree(D->getRegion());
1647 }
1648 }
1649 else {
1650 // No direct binding? Get the default binding for R (if any).
1651 if (!Xptr)
1652 Xptr = DB.lookup(R);
1653
1654 // Direct or default binding?
1655 if (Xptr) {
1656 SVal X = *Xptr;
1657 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1658
1659 // If X is a region, then add it to the RegionRoots.
1660 if (const MemRegion *RX = X.getAsRegion()) {
1661 RegionRoots.push_back(RX);
1662 // Mark the super region of the RX as live.
1663 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1664 // 'y' => element region. 'x' is its super region.
1665 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1666 RegionRoots.push_back(SR->getSuperRegion());
1667 }
1668 }
1669 }
1670 }
1671
1672 RegionStoreSubRegionMap::iterator I, E;
1673
1674 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1675 scanTree(*I);
1676}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001678void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1679 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001680 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001681{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001682 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001683 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001684
1685 // Lazily constructed backmap from MemRegions to SubRegions.
1686 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1687 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1688
Ted Kremenek9af46f52009-06-16 22:36:44 +00001689 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001690 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001691 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001692
1693 // Do a pass over the regions in the store. For VarRegions we check if
1694 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001695 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001696 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1697
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001698 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001699 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001700 const MemRegion *R = I.getKey();
1701 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001702 }
1703
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001704 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001705 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1706 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001707 I != E; ++I) {
1708 const MemRegion *R = I.getKey();
1709 IntermediateRoots.push_back(R);
1710 }
1711
1712 // Process the "intermediate" roots to find if they are referenced by
1713 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001714 while (!IntermediateRoots.empty()) {
1715 const MemRegion* R = IntermediateRoots.back();
1716 IntermediateRoots.pop_back();
1717
1718 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001719 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001720 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001721 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001722 continue;
1723 }
1724
1725 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001726 if (SymReaper.isLive(SR->getSymbol()))
1727 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001728 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001729 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001730
1731 // Add the super region for R to the worklist if it is a subregion.
1732 if (const SubRegion* superR =
1733 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1734 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001735 }
1736
1737 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1738 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001739 llvm::DenseSet<const MemRegion*> Marked;
1740 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1741 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1742 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001743
Ted Kremenek451ac092009-08-06 04:50:20 +00001744 while (!RegionRoots.empty()) {
1745 const MemRegion *R = RegionRoots.back();
1746 RegionRoots.pop_back();
1747 TS.scanTree(R);
1748 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001749
Ted Kremenek9af46f52009-06-16 22:36:44 +00001750 // We have now scanned the store, marking reachable regions and symbols
1751 // as live. We now remove all the regions that are dead from the store
1752 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001753 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001754 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001755 // If this region live? Is so, none of its symbols are dead.
1756 if (Marked.count(R))
1757 continue;
1758
1759 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001760 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001761
1762 // Mark all non-live symbols that this region references as dead.
1763 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1764 SymReaper.maybeDead(SymR->getSymbol());
1765
1766 SVal X = I.getData();
1767 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001768 for (; SI != SE; ++SI)
1769 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001770 }
1771
Ted Kremenek093569c2009-08-02 05:00:15 +00001772 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001773 RegionDefaultBindings NewDVM = DVM;
1774 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001775 state.get_context<RegionDefaultValue>();
1776
Ted Kremenek451ac092009-08-06 04:50:20 +00001777 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001778 I != E; ++I) {
1779 const MemRegion *R = I.getKey();
1780
1781 // If this region live? Is so, none of its symbols are dead.
1782 if (Marked.count(R))
1783 continue;
1784
1785 // Remove this dead region.
1786 NewDVM = DVMFactory.Remove(NewDVM, R);
1787
1788 // Mark all non-live symbols that this region references as dead.
1789 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1790 SymReaper.maybeDead(SymR->getSymbol());
1791
1792 SVal X = I.getData();
1793 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1794 for (; SI != SE; ++SI)
1795 SymReaper.maybeDead(*SI);
1796 }
1797
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001798 // Write the store back.
1799 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001800
1801 // Write the updated default bindings back.
1802 // FIXME: Right now this involves a fetching of a persistent state.
1803 // We can do better.
1804 if (DVM != NewDVM)
1805 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806}
1807
1808//===----------------------------------------------------------------------===//
1809// Utility methods.
1810//===----------------------------------------------------------------------===//
1811
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001812void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001813 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001814 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001815 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001816
Ted Kremenek451ac092009-08-06 04:50:20 +00001817 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001818 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819}