blob: 518978cfb1394a2bf818cf6a49428f26a67390f7 [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 Kremenek67f28532009-06-17 22:02:04 +0000207 SVal getLValueVar(const GRState *state, const VarDecl* VD);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000208
Ted Kremenek67f28532009-06-17 22:02:04 +0000209 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000210
Ted Kremenek67f28532009-06-17 22:02:04 +0000211 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000212
Ted Kremenek67f28532009-06-17 22:02:04 +0000213 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000214
Ted Kremenek67f28532009-06-17 22:02:04 +0000215 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000216 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000217
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000218
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000219 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
220 /// type. 'Array' represents the lvalue of the array being decayed
221 /// to a pointer, and the returned SVal represents the decayed
222 /// version of that lvalue (i.e., a pointer to the first element of
223 /// the array). This is called by GRExprEngine when evaluating
224 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000225 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000226
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000227 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000228 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000229
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000230 Store getInitialStore(const LocationContext *InitLoc) {
Zhongxing Xu0b143312009-08-21 13:25:15 +0000231 RegionBindings B = RBFactory.GetEmptyMap();
232
233 // Eagerly bind 'self' to SelfRegion, because this is the only place we can
234 // get the ObjCMethodDecl.
235 typedef LiveVariables::AnalysisDataTy LVDataTy;
236 LVDataTy &D = InitLoc->getLiveVariables()->getAnalysisData();
237 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I!=E; ++I){
238 const NamedDecl *ND = I->first;
239
240 if (const ImplicitParamDecl *PD = dyn_cast<ImplicitParamDecl>(ND)) {
241 const Decl &CD = *InitLoc->getDecl();
242 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(&CD)) {
243 if (MD->getSelfDecl() == PD) {
244 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
245 MRMgr.getHeapRegion());
246 B = RBFactory.Add(B, MRMgr.getVarRegion(PD),
247 ValMgr.makeLoc(SelfRegion));
248 }
249 }
250 }
251 }
252
253 return B.getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000254 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000255
256 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
257 /// 'this' object (C++). When used when analyzing a normal function this
258 /// method returns NULL.
259 const MemRegion* getSelfRegion(Store) {
Ted Kremenek6fd8f912009-01-22 23:43:57 +0000260 return SelfRegion;
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000261 }
Ted Kremenek67f28532009-06-17 22:02:04 +0000262
263 //===-------------------------------------------------------------------===//
264 // Binding values to regions.
265 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000266
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000267 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
268 const Expr *E, unsigned Count);
269
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000270private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000271 void RemoveSubRegionBindings(RegionBindings &B,
272 RegionDefaultBindings &DVM,
273 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000274 const MemRegion *R,
275 RegionStoreSubRegionMap &M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000276
277public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000278 const GRState *Bind(const GRState *state, Loc LV, SVal V);
279
280 const GRState *BindCompoundLiteral(const GRState *state,
281 const CompoundLiteralExpr* CL, SVal V);
282
283 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal);
284
285 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
286 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000287 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000288
Ted Kremenek67f28532009-06-17 22:02:04 +0000289 /// BindStruct - Bind a compound value to a structure.
290 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
291
292 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
293
294 /// KillStruct - Set the entire struct to unknown.
295 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
296
297 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
298
299 Store Remove(Store store, Loc LV);
300
301 //===------------------------------------------------------------------===//
302 // Loading values from regions.
303 //===------------------------------------------------------------------===//
304
305 /// The high level logic for this method is this:
306 /// Retrieve (L)
307 /// if L has binding
308 /// return L's binding
309 /// else if L is in killset
310 /// return unknown
311 /// else
312 /// if L is on stack or heap
313 /// return undefined
314 /// else
315 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000316 SValuator::CastResult Retrieve(const GRState *state, Loc L,
317 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000318
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000319 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000320
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000321 SVal RetrieveField(const GRState *state, const FieldRegion *R);
322
323 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000324
Ted Kremenek9031dd72009-07-21 00:12:07 +0000325 SVal RetrieveVar(const GRState *state, const VarRegion *R);
326
Ted Kremenek25c54572009-07-20 22:58:02 +0000327 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
328
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000329 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
330 QualType Ty, const MemRegion *superR);
331
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000332 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
333 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000334
Ted Kremenek67f28532009-06-17 22:02:04 +0000335 /// Retrieve the values in a struct and return a CompoundVal, used when doing
336 /// struct copy:
337 /// struct s x, y;
338 /// x = y;
339 /// y's value is retrieved by this method.
340 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
341
342 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000343
344 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000345 GetLazyBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000346
347 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
348 const GRState *state,
349 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000350
351 //===------------------------------------------------------------------===//
352 // State pruning.
353 //===------------------------------------------------------------------===//
354
355 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
356 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000357 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000358 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
359
360 //===------------------------------------------------------------------===//
361 // Region "extents".
362 //===------------------------------------------------------------------===//
363
364 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
365 SVal getSizeInElements(const GRState *state, const MemRegion* R);
366
367 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000368 // Utility methods.
369 //===------------------------------------------------------------------===//
370
Ted Kremenek451ac092009-08-06 04:50:20 +0000371 static inline RegionBindings GetRegionBindings(Store store) {
372 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000373 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000374
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000375 void print(Store store, llvm::raw_ostream& Out, const char* nl,
376 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000377
378 void iterBindings(Store store, BindingsHandler& f) {
379 // FIXME: Implement.
380 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000381
Ted Kremenek67f28532009-06-17 22:02:04 +0000382 // FIXME: Remove.
383 BasicValueFactory& getBasicVals() {
384 return StateMgr.getBasicVals();
385 }
386
387 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000388 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000389};
390
391} // end anonymous namespace
392
Ted Kremenek9af46f52009-06-16 22:36:44 +0000393//===----------------------------------------------------------------------===//
394// RegionStore creation.
395//===----------------------------------------------------------------------===//
396
397StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
398 RegionStoreFeatures F = maximal_features_tag();
399 return new RegionStoreManager(StMgr, F);
400}
401
402StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
403 RegionStoreFeatures F = minimal_features_tag();
404 F.enableFields(true);
405 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000406}
407
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000408void
409RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
410 const SubRegion *R) {
411 const MemRegion *superR = R->getSuperRegion();
412 if (add(superR, R))
413 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
414 WL.push_back(sr);
415}
416
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000417RegionStoreSubRegionMap*
418RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000419 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000420 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
421
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000422 llvm::SmallVector<const SubRegion*, 10> WL;
423
Ted Kremenek451ac092009-08-06 04:50:20 +0000424 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000425 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
426 M->process(WL, R);
427
Ted Kremenek451ac092009-08-06 04:50:20 +0000428 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
429 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430 I != E; ++I)
431 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
432 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000433
434 // We also need to record in the subregion map "intermediate" regions that
435 // don't have direct bindings but are super regions of those that do.
436 while (!WL.empty()) {
437 const SubRegion *R = WL.back();
438 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000439 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000440 }
441
Ted Kremenek14453bf2009-03-03 19:02:42 +0000442 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000443}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000444
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
446 return getRegionStoreSubRegionMap(state);
447}
448
Ted Kremenek9af46f52009-06-16 22:36:44 +0000449//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000450// Binding invalidation.
451//===----------------------------------------------------------------------===//
452
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000453void
Ted Kremenek451ac092009-08-06 04:50:20 +0000454RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
455 RegionDefaultBindings &DVM,
456 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000457 const MemRegion *R,
458 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000459
460 RegionStoreSubRegionMap::iterator I, E;
461
462 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000463 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000464
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000465 B = RBFactory.Remove(B, R);
466 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000467}
468
469
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000470const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
471 const MemRegion *R,
472 const Expr *E,
473 unsigned Count) {
474 ASTContext& Ctx = StateMgr.getContext();
475
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000476 // Strip away casts.
477 R = R->getBaseRegion();
478
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000479 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000480 {
481 // Get the mapping of regions -> subregions.
482 llvm::OwningPtr<RegionStoreSubRegionMap>
483 SubRegions(getRegionStoreSubRegionMap(state));
484
Ted Kremenek451ac092009-08-06 04:50:20 +0000485 RegionBindings B = GetRegionBindings(state->getStore());
486 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
487 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000488 state->get_context<RegionDefaultValue>();
489
490 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
491 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
492 }
493
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000494 if (!R->isBoundable())
495 return state;
496
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000497 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
498 isa<ObjCObjectRegion>(R)) {
499 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000500 // conjured symbol. The type of the symbol is irrelavant.
501 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000502 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000503 }
504
505 const TypedRegion *TR = cast<TypedRegion>(R);
506 QualType T = TR->getValueType(Ctx);
507
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000508 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000509 // FIXME: handle structs with default region value.
510 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
511
512 // No record definition. There is nothing we can do.
513 if (!RD)
514 return state;
515
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000516 // Invalidate the region by setting its default value to
517 // conjured symbol. The type of the symbol is irrelavant.
518 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
519 return setDefaultValue(state, R, V);
520 }
521
522 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000523 // Set the default value of the array to conjured symbol.
524 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
525 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000526 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000527 }
528
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000529 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
530 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
531 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000532}
533
534//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000535// getLValueXXX methods.
536//===----------------------------------------------------------------------===//
537
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000538/// getLValueString - Returns an SVal representing the lvalue of a
539/// StringLiteral. Within RegionStore a StringLiteral has an
540/// associated StringRegion, and the lvalue of a StringLiteral is the
541/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000542SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000543 const StringLiteral* S) {
544 return loc::MemRegionVal(MRMgr.getStringRegion(S));
545}
546
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000547/// getLValueVar - Returns an SVal that represents the lvalue of a
548/// variable. Within RegionStore a variable has an associated
549/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000550SVal RegionStoreManager::getLValueVar(const GRState *St, const VarDecl* VD) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000551 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
552}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000553
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000554/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
555/// of a compound literal. Within RegionStore a compound literal
556/// has an associated region, and the lvalue of the compound literal
557/// is the lvalue of that region.
558SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000559RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000560 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000561 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
562}
563
Ted Kremenek67f28532009-06-17 22:02:04 +0000564SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000565 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000566 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000567}
568
Ted Kremenek67f28532009-06-17 22:02:04 +0000569SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000570 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000571 return getLValueFieldOrIvar(St, Base, D);
572}
573
Ted Kremenek67f28532009-06-17 22:02:04 +0000574SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000575 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000576 if (Base.isUnknownOrUndef())
577 return Base;
578
579 Loc BaseL = cast<Loc>(Base);
580 const MemRegion* BaseR = 0;
581
582 switch (BaseL.getSubKind()) {
583 case loc::MemRegionKind:
584 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
585 break;
586
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000587 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000588 // These are anormal cases. Flag an undefined value.
589 return UndefinedVal();
590
591 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592 // While these seem funny, this can happen through casts.
593 // FIXME: What we should return is the field offset. For example,
594 // add the field offset to the integer value. That way funny things
595 // like this work properly: &(((struct foo *) 0xa)->f)
596 return Base;
597
598 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000599 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000600 return Base;
601 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000602
603 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
604 // of FieldDecl.
605 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
606 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000607
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000608 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000609}
610
Ted Kremenek67f28532009-06-17 22:02:04 +0000611SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000612 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000613 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000614
Ted Kremenekde7ec632009-03-09 22:44:49 +0000615 // If the base is an unknown or undefined value, just return it back.
616 // FIXME: For absolute pointer addresses, we just return that value back as
617 // well, although in reality we should return the offset added to that
618 // value.
619 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000620 return Base;
621
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000622 // Only handle integer offsets... for now.
623 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000624 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000625
Zhongxing Xuce760782009-05-09 13:20:07 +0000626 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000627
628 // Pointer of any type can be cast and used as array base.
629 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
630
Ted Kremenek46537392009-07-16 01:33:37 +0000631 // Convert the offset to the appropriate size and signedness.
632 Offset = ValMgr.convertToArrayIndex(Offset);
633
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000634 if (!ElemR) {
635 //
636 // If the base region is not an ElementRegion, create one.
637 // This can happen in the following example:
638 //
639 // char *p = __builtin_alloc(10);
640 // p[1] = 8;
641 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000642 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000643 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000644 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000645 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000646 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000647
648 SVal BaseIdx = ElemR->getIndex();
649
650 if (!isa<nonloc::ConcreteInt>(BaseIdx))
651 return UnknownVal();
652
653 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
654 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
655 assert(BaseIdxI.isSigned());
656
Ted Kremenek46537392009-07-16 01:33:37 +0000657 // Compute the new index.
658 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000659
Ted Kremenek46537392009-07-16 01:33:37 +0000660 // Construct the new ElementRegion.
661 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000662 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
663 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000664}
665
Ted Kremenek9af46f52009-06-16 22:36:44 +0000666//===----------------------------------------------------------------------===//
667// Extents for regions.
668//===----------------------------------------------------------------------===//
669
Ted Kremenek67f28532009-06-17 22:02:04 +0000670SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000671 const MemRegion *R) {
672
673 switch (R->getKind()) {
674 case MemRegion::MemSpaceRegionKind:
675 assert(0 && "Cannot index into a MemSpace");
676 return UnknownVal();
677
678 case MemRegion::CodeTextRegionKind:
679 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000680 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000681
682 // Not yet handled.
683 case MemRegion::AllocaRegionKind:
684 case MemRegion::CompoundLiteralRegionKind:
685 case MemRegion::ElementRegionKind:
686 case MemRegion::FieldRegionKind:
687 case MemRegion::ObjCIvarRegionKind:
688 case MemRegion::ObjCObjectRegionKind:
689 case MemRegion::SymbolicRegionKind:
690 return UnknownVal();
691
692 case MemRegion::StringRegionKind: {
693 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
694 // We intentionally made the size value signed because it participates in
695 // operations with signed indices.
696 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000697 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000698
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000699 case MemRegion::VarRegionKind: {
700 const VarRegion* VR = cast<VarRegion>(R);
701 // Get the type of the variable.
702 QualType T = VR->getDesugaredValueType(getContext());
703
704 // FIXME: Handle variable-length arrays.
705 if (isa<VariableArrayType>(T))
706 return UnknownVal();
707
708 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
709 // return the size as signed integer.
710 return ValMgr.makeIntVal(CAT->getSize(), false);
711 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000712
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000713 // Clients can use ordinary variables as if they were arrays. These
714 // essentially are arrays of size 1.
715 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000716 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717
718 case MemRegion::BEG_DECL_REGIONS:
719 case MemRegion::END_DECL_REGIONS:
720 case MemRegion::BEG_TYPED_REGIONS:
721 case MemRegion::END_TYPED_REGIONS:
722 assert(0 && "Infeasible region");
723 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000724 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000725
726 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000727 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000728}
729
Ted Kremenek67f28532009-06-17 22:02:04 +0000730const GRState *RegionStoreManager::setExtent(const GRState *state,
731 const MemRegion *region,
732 SVal extent) {
733 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000734}
735
736//===----------------------------------------------------------------------===//
737// Location and region casting.
738//===----------------------------------------------------------------------===//
739
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000740/// ArrayToPointer - Emulates the "decay" of an array to a pointer
741/// type. 'Array' represents the lvalue of the array being decayed
742/// to a pointer, and the returned SVal represents the decayed
743/// version of that lvalue (i.e., a pointer to the first element of
744/// the array). This is called by GRExprEngine when evaluating casts
745/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000746SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000747 if (!isa<loc::MemRegionVal>(Array))
748 return UnknownVal();
749
750 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
751 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
752
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000753 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000754 return UnknownVal();
755
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000756 // Strip off typedefs from the ArrayRegion's ValueType.
757 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000758 ArrayType *AT = cast<ArrayType>(T);
759 T = AT->getElementType();
760
Ted Kremenek75185b52009-07-16 00:00:11 +0000761 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
762 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000763
764 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000765}
766
Ted Kremenek9af46f52009-06-16 22:36:44 +0000767//===----------------------------------------------------------------------===//
768// Pointer arithmetic.
769//===----------------------------------------------------------------------===//
770
Zhongxing Xu262fd032009-05-20 09:00:16 +0000771SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000772 BinaryOperator::Opcode Op, Loc L, NonLoc R,
773 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000774 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000775 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000776 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000777
Zhongxing Xua1718c72009-04-03 07:33:13 +0000778 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000779 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000780
Ted Kremenek3bccf082009-07-11 00:58:27 +0000781 switch (MR->getKind()) {
782 case MemRegion::SymbolicRegionKind: {
783 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000784 SymbolRef Sym = SR->getSymbol();
785 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000786 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000787 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
788 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
789 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000790 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000791 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000792 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000793 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000794 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000795 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
796 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
797 break;
798 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000799
Ted Kremenek3bccf082009-07-11 00:58:27 +0000800 case MemRegion::ElementRegionKind: {
801 ER = cast<ElementRegion>(MR);
802 break;
803 }
804
805 // Not yet handled.
806 case MemRegion::VarRegionKind:
807 case MemRegion::StringRegionKind:
808 case MemRegion::CompoundLiteralRegionKind:
809 case MemRegion::FieldRegionKind:
810 case MemRegion::ObjCObjectRegionKind:
811 case MemRegion::ObjCIvarRegionKind:
812 return UnknownVal();
813
Ted Kremenek3bccf082009-07-11 00:58:27 +0000814 case MemRegion::CodeTextRegionKind:
815 // Technically this can happen if people do funny things with casts.
816 return UnknownVal();
817
818 case MemRegion::MemSpaceRegionKind:
819 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
820 return UnknownVal();
821
822 case MemRegion::BEG_DECL_REGIONS:
823 case MemRegion::END_DECL_REGIONS:
824 case MemRegion::BEG_TYPED_REGIONS:
825 case MemRegion::END_TYPED_REGIONS:
826 assert(0 && "Infeasible region");
827 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000828 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000829
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000830 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000831 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
832 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
833
834 // Only support concrete integer indexes for now.
835 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000836 // FIXME: Should use SValuator here.
837 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
838 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000839 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000840 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
841 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000842 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000843 }
844
845 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000846}
847
Ted Kremenek9af46f52009-06-16 22:36:44 +0000848//===----------------------------------------------------------------------===//
849// Loading values from regions.
850//===----------------------------------------------------------------------===//
851
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000852Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
853 const MemRegion *R) {
854
855 if (R->isBoundable())
856 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
857 if (TR->getValueType(getContext())->isUnionType())
858 return UnknownVal();
859
860 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
861}
862
Ted Kremeneka6275a52009-07-15 02:31:43 +0000863static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
864 RTy = Ctx.getCanonicalType(RTy);
865 UsedTy = Ctx.getCanonicalType(UsedTy);
866
867 if (RTy == UsedTy)
868 return false;
869
Ted Kremenek25c54572009-07-20 22:58:02 +0000870
871 // Recursively check the types. We basically want to see if a pointer value
872 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
873 // represents a reinterpretation.
874 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000875 const PointerType *PRTy = RTy->getAs<PointerType>();
876 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000877
878 return PUsedTy && PRTy &&
879 IsReinterpreted(PRTy->getPointeeType(),
880 PUsedTy->getPointeeType(), Ctx);
881 }
882
883 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000884}
885
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000886SValuator::CastResult
887RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000888
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000889 assert(!isa<UnknownVal>(L) && "location unknown");
890 assert(!isa<UndefinedVal>(L) && "location undefined");
891
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000892 // FIXME: Is this even possible? Shouldn't this be treated as a null
893 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000894 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000895 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000896
Ted Kremenek67f28532009-06-17 22:02:04 +0000897 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000898
Zhongxing Xu91844122009-05-20 09:18:48 +0000899 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000900 // Example:
901 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000902 // char* p = alloca();
903 // read(p);
904 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000905 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000906 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000907
908 if (isa<SymbolicRegion>(MR)) {
909 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000910 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000911 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000912 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
913 }
914
Ted Kremenek968f0a62009-08-03 21:41:46 +0000915 if (isa<CodeTextRegion>(MR))
916 return SValuator::CastResult(state, UnknownVal());
917
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000918 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
919 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000920 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000921 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000922
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000923 // FIXME: We should eventually handle funny addressing. e.g.:
924 //
925 // int x = ...;
926 // int *p = &x;
927 // char *q = (char*) p;
928 // char c = *q; // returns the first byte of 'x'.
929 //
930 // Such funny addressing will occur due to layering of regions.
931
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000932#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000933 ASTContext &Ctx = getContext();
934 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000935 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
936 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000937 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000938 assert(Ctx.getCanonicalType(RTy) ==
939 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000940 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000941#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000942
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000943 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000944 return SValuator::CastResult(state, RetrieveStruct(state, R));
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000945
946 // FIXME: Handle unions.
947 if (RTy->isUnionType())
948 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000949
950 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000951 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000952
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000953 // FIXME: handle Vector types.
954 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000955 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000956
957 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000958 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000959
960 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000961 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000962
Ted Kremenek25c54572009-07-20 22:58:02 +0000963 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000964 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000965
966 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000967 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000968
Ted Kremenek451ac092009-08-06 04:50:20 +0000969 RegionBindings B = GetRegionBindings(state->getStore());
970 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000971
972 // Check if the region has a binding.
973 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000974 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000975
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000976 // The location does not have a bound value. This means that it has
977 // the value it had upon its creation and/or entry to the analyzed
978 // function/method. These are either symbolic values or 'undefined'.
979
Ted Kremenek356e9d62009-07-22 04:35:42 +0000980#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000981 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000982#else
983 if (R->hasStackStorage()) {
984#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000985 // All stack variables are considered to have undefined values
986 // upon creation. All heap allocated blocks are considered to
987 // have undefined values as well unless they are explicitly bound
988 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000989 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000990 }
991
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000992 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000993 return SValuator::CastResult(state,
994 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000995}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000996
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000997std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000998RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000999
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001000 if (const nonloc::LazyCompoundVal *V =
1001 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
1002 return std::make_pair(V->getState(), V->getRegion());
1003
1004 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1005 const std::pair<const GRState *, const MemRegion *> &X =
1006 GetLazyBinding(B, ER->getSuperRegion());
1007
1008 if (X.first)
1009 return std::make_pair(X.first,
1010 MRMgr.getElementRegionWithSuper(ER, X.second));
1011 }
1012 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1013 const std::pair<const GRState *, const MemRegion *> &X =
1014 GetLazyBinding(B, FR->getSuperRegion());
1015
1016 if (X.first)
1017 return std::make_pair(X.first,
1018 MRMgr.getFieldRegionWithSuper(FR, X.second));
1019 }
1020
1021 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1022}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001023
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001024SVal RegionStoreManager::RetrieveElement(const GRState* state,
1025 const ElementRegion* R) {
1026 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001027 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001028 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001029 return *V;
1030
Ted Kremenek921109a2009-07-01 23:19:52 +00001031 const MemRegion* superR = R->getSuperRegion();
1032
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001033 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001034 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001035 const StringLiteral *Str = StrR->getStringLiteral();
1036 SVal Idx = R->getIndex();
1037 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1038 int64_t i = CI->getValue().getSExtValue();
1039 char c;
1040 if (i == Str->getByteLength())
1041 c = '\0';
1042 else
1043 c = Str->getStrData()[i];
1044 return ValMgr.makeIntVal(c, getContext().CharTy);
1045 }
1046 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001047
1048 // Special case: the current region represents a cast and it and the super
1049 // region both have pointer types or intptr_t types. If so, perform the
1050 // retrieve from the super region and appropriately "cast" the value.
1051 // This is needed to support OSAtomicCompareAndSwap and friends or other
1052 // loads that treat integers as pointers and vis versa.
1053 if (R->getIndex().isZeroConstant()) {
1054 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1055 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001056 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1057 QualType valTy = R->getValueType(Ctx);
1058 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1059 // Retrieve the value from the super region. This will be casted to
1060 // valTy when we return to 'Retrieve'.
1061 const SValuator::CastResult &cr = Retrieve(state,
1062 loc::MemRegionVal(superR),
1063 valTy);
1064 return cr.getSVal();
1065 }
1066 }
1067 }
1068 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001069
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001070 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001071 if (const SVal *V = B.lookup(superR)) {
1072 if (SymbolRef parentSym = V->getAsSymbol())
1073 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001074
1075 if (V->isUnknownOrUndef())
1076 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001077
1078 // Handle LazyCompoundVals for the immediate super region. Other cases
1079 // are handled in 'RetrieveFieldOrElementCommon'.
1080 if (const nonloc::LazyCompoundVal *LCV =
1081 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001082
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001083 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1084 return RetrieveElement(LCV->getState(), R);
1085 }
1086
Ted Kremeneka6275a52009-07-15 02:31:43 +00001087 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001088 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001089 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001090
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001091 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001092}
1093
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001094SVal RegionStoreManager::RetrieveField(const GRState* state,
1095 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001096
1097 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001098 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001099 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001100 return *V;
1101
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001102 QualType Ty = R->getValueType(getContext());
1103 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1104}
1105
1106SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1107 const TypedRegion *R,
1108 QualType Ty,
1109 const MemRegion *superR) {
1110
1111 // At this point we have already checked in either RetrieveElement or
1112 // RetrieveField if 'R' has a direct binding.
1113
1114 RegionBindings B = GetRegionBindings(state->getStore());
1115
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001116 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001117 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001118 if (SymbolRef parentSym = D->getAsSymbol())
1119 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001120
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001121 if (D->isZeroConstant())
1122 return ValMgr.makeZeroVal(Ty);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001123
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001124 if (D->isUnknown())
1125 return *D;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001126
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001127 assert(0 && "Unknown default value");
1128 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001129
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001130 // If our super region is a field or element itself, walk up the region
1131 // hierarchy to see if there is a default value installed in an ancestor.
1132 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1133 superR = cast<SubRegion>(superR)->getSuperRegion();
1134 continue;
1135 }
1136
1137 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001138 }
1139
1140 // Lazy binding?
1141 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001142 const MemRegion *lazyBindingRegion = NULL;
1143 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001144
1145 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001146 assert(lazyBindingRegion && "Lazy-binding region not set");
1147
1148 if (isa<ElementRegion>(R))
1149 return RetrieveElement(lazyBindingState,
1150 cast<ElementRegion>(lazyBindingRegion));
1151
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001152 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001153 cast<FieldRegion>(lazyBindingRegion));
1154 }
Ted Kremenekdc147262009-07-02 22:02:15 +00001155
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001156 if (R->hasStackStorage() && !R->hasParametersStorage()) {
1157
1158 if (isa<ElementRegion>(R)) {
1159 // Currently we don't reason specially about Clang-style vectors. Check
1160 // if superR is a vector and if so return Unknown.
1161 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1162 if (typedSuperR->getValueType(getContext())->isVectorType())
1163 return UnknownVal();
1164 }
1165 }
1166
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001167 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001168 }
1169
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001170 // All other values are symbolic.
1171 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001172}
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001173
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001174SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1175 const ObjCIvarRegion* R) {
1176
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001177 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001178 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001179
1180 if (const SVal* V = B.lookup(R))
1181 return *V;
1182
1183 const MemRegion *superR = R->getSuperRegion();
1184
1185 // Check if the super region has a binding.
1186 if (const SVal *V = B.lookup(superR)) {
1187 if (SymbolRef parentSym = V->getAsSymbol())
1188 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1189
1190 // Other cases: give up.
1191 return UnknownVal();
1192 }
1193
Ted Kremenek25c54572009-07-20 22:58:02 +00001194 return RetrieveLazySymbol(state, R);
1195}
1196
Ted Kremenek9031dd72009-07-21 00:12:07 +00001197SVal RegionStoreManager::RetrieveVar(const GRState *state,
1198 const VarRegion *R) {
1199
1200 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001201 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek9031dd72009-07-21 00:12:07 +00001202
1203 if (const SVal* V = B.lookup(R))
1204 return *V;
1205
1206 // Lazily derive a value for the VarRegion.
1207 const VarDecl *VD = R->getDecl();
1208
Ted Kremenek9031dd72009-07-21 00:12:07 +00001209 if (R->hasGlobalsOrParametersStorage())
1210 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1211
1212 return UndefinedVal();
1213}
1214
Ted Kremenek25c54572009-07-20 22:58:02 +00001215SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1216 const TypedRegion *R) {
1217
1218 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001219
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001220 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001221 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001222}
1223
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001224SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1225 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001226 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001227 assert(T->isStructureType());
1228
Zhongxing Xub7507d12009-06-11 07:27:30 +00001229 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001230 RecordDecl* RD = RT->getDecl();
1231 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001232 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001233#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001234 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1235
Ted Kremenek67f28532009-06-17 22:02:04 +00001236 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1237 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001238 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001239
Douglas Gregore267ff32008-12-11 20:41:00 +00001240 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1241 FieldEnd = Fields.rend();
1242 Field != FieldEnd; ++Field) {
1243 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001244 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001245 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001246 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1247 }
1248
Zhongxing Xud91ee272009-06-23 09:02:15 +00001249 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001250#else
1251 return ValMgr.makeLazyCompoundVal(state, R);
1252#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001253}
1254
Ted Kremenek67f28532009-06-17 22:02:04 +00001255SVal RegionStoreManager::RetrieveArray(const GRState *state,
1256 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001257#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001258 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001259 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1260
1261 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001262 uint64_t size = CAT->getSize().getZExtValue();
1263 for (uint64_t i = 0; i < size; ++i) {
1264 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001265 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1266 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001267 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001268 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001269 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1270 }
1271
Zhongxing Xud91ee272009-06-23 09:02:15 +00001272 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001273#else
1274 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1275 return ValMgr.makeLazyCompoundVal(state, R);
1276#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001277}
1278
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001279SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1280 const GRState *state,
1281 const TypedRegion *R,
1282 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001284 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001285
1286 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001287 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001288}
1289
Ted Kremenek9af46f52009-06-16 22:36:44 +00001290//===----------------------------------------------------------------------===//
1291// Binding values to regions.
1292//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001293
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001294Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001295 const MemRegion* R = 0;
1296
1297 if (isa<loc::MemRegionVal>(L))
1298 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001299
1300 if (R) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001301 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001302 return RBFactory.Remove(B, R).getRoot();
1303 }
1304
1305 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001306}
1307
Ted Kremenek67f28532009-06-17 22:02:04 +00001308const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001309 if (isa<loc::ConcreteInt>(L))
1310 return state;
1311
Ted Kremenek9af46f52009-06-16 22:36:44 +00001312 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001313 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001314
1315 // Check if the region is a struct region.
1316 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1317 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001318 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001319
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001320 // Special case: the current region represents a cast and it and the super
1321 // region both have pointer types or intptr_t types. If so, perform the
1322 // bind to the super region.
1323 // This is needed to support OSAtomicCompareAndSwap and friends or other
1324 // loads that treat integers as pointers and vis versa.
1325 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1326 if (ER->getIndex().isZeroConstant()) {
1327 if (const TypedRegion *superR =
1328 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1329 ASTContext &Ctx = getContext();
1330 QualType superTy = superR->getValueType(Ctx);
1331 QualType erTy = ER->getValueType(Ctx);
1332
1333 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1334 IsAnyPointerOrIntptr(erTy, Ctx)) {
1335 SValuator::CastResult cr =
1336 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1337 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1338 }
1339 }
1340 }
1341 }
1342
1343 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001344 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001345 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001346}
1347
Ted Kremenek67f28532009-06-17 22:02:04 +00001348const GRState *RegionStoreManager::BindDecl(const GRState *state,
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001349 const VarDecl* VD, SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001350
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001351 QualType T = VD->getType();
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001352 VarRegion* VR = MRMgr.getVarRegion(VD);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001353
Ted Kremenek0964a062009-01-21 06:57:53 +00001354 if (T->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001355 return BindArray(state, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001356 if (T->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001357 return BindStruct(state, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001358
Zhongxing Xud91ee272009-06-23 09:02:15 +00001359 return Bind(state, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001360}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001361
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001362// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001363const GRState *
1364RegionStoreManager::BindCompoundLiteral(const GRState *state,
1365 const CompoundLiteralExpr* CL,
1366 SVal V) {
1367
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001368 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001369 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001370}
1371
Ted Kremenek67f28532009-06-17 22:02:04 +00001372const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001373 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001374 SVal Init) {
1375
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001376 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001377 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001378 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001379
Ted Kremenek46537392009-07-16 01:33:37 +00001380 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001381
1382 // Check if the init expr is a StringLiteral.
1383 if (isa<loc::MemRegionVal>(Init)) {
1384 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1385 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1386 const char* str = S->getStrData();
1387 unsigned len = S->getByteLength();
1388 unsigned j = 0;
1389
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001390 // Copy bytes from the string literal into the target array. Trailing bytes
1391 // in the array that are not covered by the string literal are initialized
1392 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001393 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001394 if (j >= len)
1395 break;
1396
Ted Kremenek46537392009-07-16 01:33:37 +00001397 SVal Idx = ValMgr.makeArrayIndex(i);
1398 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1399 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001400
Zhongxing Xud91ee272009-06-23 09:02:15 +00001401 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001402 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001403 }
1404
Ted Kremenek67f28532009-06-17 22:02:04 +00001405 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001406 }
1407
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001408 // Handle lazy compound values.
1409 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1410 return CopyLazyBindings(*LCV, state, R);
1411
1412 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001413 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001414 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001415 uint64_t i = 0;
1416
1417 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001418 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001419 if (VI == VE)
1420 break;
1421
Ted Kremenek46537392009-07-16 01:33:37 +00001422 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001423 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001424
1425 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001426 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001427 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001428 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001429 }
1430
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001431 // If the init list is shorter than the array length, set the array default
1432 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001433 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001434 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001435 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001436 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001437 }
1438 }
1439
Ted Kremenek67f28532009-06-17 22:02:04 +00001440 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001441}
1442
Ted Kremenek67f28532009-06-17 22:02:04 +00001443const GRState *
1444RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1445 SVal V) {
1446
1447 if (!Features.supportsFields())
1448 return state;
1449
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001450 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001451 assert(T->isStructureType());
1452
Ted Kremenek6217b802009-07-29 21:53:49 +00001453 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001454 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001455
1456 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001457 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001458
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001459 // Handle lazy compound values.
1460 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1461 return CopyLazyBindings(*LCV, state, R);
1462
Ted Kremenek67f28532009-06-17 22:02:04 +00001463 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1464 // Ignore them and kill the field values.
1465 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1466 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001467
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001468 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001469 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001470
1471 RecordDecl::field_iterator FI, FE;
1472
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001473 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001474
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001475 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001476 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001477
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001478 QualType FTy = (*FI)->getType();
1479 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1480
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001481 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001482 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001483 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001484 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001485 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001486 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001487 }
1488
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001489 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001490 if (FI != FE)
1491 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001492
Ted Kremenek67f28532009-06-17 22:02:04 +00001493 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001494}
1495
Ted Kremenek67f28532009-06-17 22:02:04 +00001496const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001497 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001498
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001499 // Set the default value of the struct region to "unknown".
1500 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001501
1502 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001503 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001504 RegionBindings B = GetRegionBindings(store);
1505 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001506 const MemRegion* R = I.getKey();
1507 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1508 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001509 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001510 }
1511
Ted Kremenek67f28532009-06-17 22:02:04 +00001512 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001513}
1514
Ted Kremenek67f28532009-06-17 22:02:04 +00001515const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1516 const MemRegion* R, SVal V) {
1517 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001518}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001519
1520const GRState*
1521RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1522 const GRState *state,
1523 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001524
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001525 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001526 RegionBindings B = GetRegionBindings(state->getStore());
1527 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1528 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001529 state->get_context<RegionDefaultValue>();
1530
1531 llvm::OwningPtr<RegionStoreSubRegionMap>
1532 SubRegions(getRegionStoreSubRegionMap(state));
1533
1534 // B and DVM are updated after the call to RemoveSubRegionBindings.
1535 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1536
1537 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1538 // results in a zero-copy algorithm.
1539 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1540}
1541
Ted Kremenek9af46f52009-06-16 22:36:44 +00001542//===----------------------------------------------------------------------===//
1543// State pruning.
1544//===----------------------------------------------------------------------===//
Ted Kremenek451ac092009-08-06 04:50:20 +00001545
Ted Kremenek9af46f52009-06-16 22:36:44 +00001546static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1547 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1548 const MemRegion *R = XR->getRegion();
1549
1550 while (R) {
1551 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1552 SymReaper.markLive(SR->getSymbol());
1553 return;
1554 }
1555
1556 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1557 R = SR->getSuperRegion();
1558 continue;
1559 }
1560
1561 break;
1562 }
1563
1564 return;
1565 }
1566
1567 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1568 SymReaper.markLive(*SI);
1569}
Ted Kremenek451ac092009-08-06 04:50:20 +00001570
1571namespace {
1572class VISIBILITY_HIDDEN TreeScanner {
1573 RegionBindings B;
1574 RegionDefaultBindings DB;
1575 SymbolReaper &SymReaper;
1576 llvm::DenseSet<const MemRegion*> &Marked;
1577 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1578 RegionStoreSubRegionMap &M;
1579 RegionStoreManager &RS;
1580 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1581 const bool MarkKeys;
1582public:
1583 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1584 SymbolReaper &symReaper,
1585 llvm::DenseSet<const MemRegion*> &marked,
1586 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1587 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1588 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1589 bool markKeys = true)
1590 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1591 ScannedLazyVals(scannedLazyVals), M(m),
1592 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
1593
1594 void scanTree(const MemRegion *R);
1595};
1596} // end anonymous namespace
1597
1598
1599void TreeScanner::scanTree(const MemRegion *R) {
1600 if (MarkKeys) {
1601 if (Marked.count(R))
1602 return;
1603
1604 Marked.insert(R);
1605 }
1606
1607 // Mark the symbol for any live SymbolicRegion as "live". This means we
1608 // should continue to track that symbol.
1609 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1610 SymReaper.markLive(SymR->getSymbol());
1611
1612 // Get the data binding for R (if any).
1613 const SVal* Xptr = B.lookup(R);
1614
1615 // Check for lazy bindings.
1616 if (const nonloc::LazyCompoundVal *V =
1617 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1618
1619 const LazyCompoundValData *D = V->getCVData();
1620
1621 if (!ScannedLazyVals.count(D)) {
1622 // Scan the bindings in the LazyCompoundVal.
1623 ScannedLazyVals.insert(D);
1624
1625 // FIXME: Cache subregion maps.
1626 const GRState *lazyState = D->getState();
1627
1628 llvm::OwningPtr<RegionStoreSubRegionMap>
1629 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
1630
1631 Store lazyStore = lazyState->getStore();
1632 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
1633
1634 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
1635
1636 // Scan the bindings.
1637 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1638 *lazySM.get(), RS, RegionRoots, false);
1639
1640 scan.scanTree(D->getRegion());
1641 }
1642 }
1643 else {
1644 // No direct binding? Get the default binding for R (if any).
1645 if (!Xptr)
1646 Xptr = DB.lookup(R);
1647
1648 // Direct or default binding?
1649 if (Xptr) {
1650 SVal X = *Xptr;
1651 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1652
1653 // If X is a region, then add it to the RegionRoots.
1654 if (const MemRegion *RX = X.getAsRegion()) {
1655 RegionRoots.push_back(RX);
1656 // Mark the super region of the RX as live.
1657 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1658 // 'y' => element region. 'x' is its super region.
1659 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1660 RegionRoots.push_back(SR->getSuperRegion());
1661 }
1662 }
1663 }
1664 }
1665
1666 RegionStoreSubRegionMap::iterator I, E;
1667
1668 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1669 scanTree(*I);
1670}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001671
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001672void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1673 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001674 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001675{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001676 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001677 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001678
1679 // Lazily constructed backmap from MemRegions to SubRegions.
1680 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1681 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1682
Ted Kremenek9af46f52009-06-16 22:36:44 +00001683 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001684 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001685 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001686
1687 // Do a pass over the regions in the store. For VarRegions we check if
1688 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001689 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001690 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1691
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001692 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001693 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001694 const MemRegion *R = I.getKey();
1695 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001696 }
1697
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001698 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001699 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1700 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001701 I != E; ++I) {
1702 const MemRegion *R = I.getKey();
1703 IntermediateRoots.push_back(R);
1704 }
1705
1706 // Process the "intermediate" roots to find if they are referenced by
1707 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001708 while (!IntermediateRoots.empty()) {
1709 const MemRegion* R = IntermediateRoots.back();
1710 IntermediateRoots.pop_back();
1711
1712 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001713 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001714 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001715 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001716 continue;
1717 }
1718
1719 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001720 if (SymReaper.isLive(SR->getSymbol()))
1721 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001722 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001723 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001724
1725 // Add the super region for R to the worklist if it is a subregion.
1726 if (const SubRegion* superR =
1727 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1728 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001729 }
1730
1731 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1732 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001733 llvm::DenseSet<const MemRegion*> Marked;
1734 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1735 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1736 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001737
Ted Kremenek451ac092009-08-06 04:50:20 +00001738 while (!RegionRoots.empty()) {
1739 const MemRegion *R = RegionRoots.back();
1740 RegionRoots.pop_back();
1741 TS.scanTree(R);
1742 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001743
Ted Kremenek9af46f52009-06-16 22:36:44 +00001744 // We have now scanned the store, marking reachable regions and symbols
1745 // as live. We now remove all the regions that are dead from the store
1746 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001747 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001748 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001749 // If this region live? Is so, none of its symbols are dead.
1750 if (Marked.count(R))
1751 continue;
1752
1753 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001754 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001755
1756 // Mark all non-live symbols that this region references as dead.
1757 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1758 SymReaper.maybeDead(SymR->getSymbol());
1759
1760 SVal X = I.getData();
1761 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001762 for (; SI != SE; ++SI)
1763 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001764 }
1765
Ted Kremenek093569c2009-08-02 05:00:15 +00001766 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001767 RegionDefaultBindings NewDVM = DVM;
1768 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001769 state.get_context<RegionDefaultValue>();
1770
Ted Kremenek451ac092009-08-06 04:50:20 +00001771 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001772 I != E; ++I) {
1773 const MemRegion *R = I.getKey();
1774
1775 // If this region live? Is so, none of its symbols are dead.
1776 if (Marked.count(R))
1777 continue;
1778
1779 // Remove this dead region.
1780 NewDVM = DVMFactory.Remove(NewDVM, R);
1781
1782 // Mark all non-live symbols that this region references as dead.
1783 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1784 SymReaper.maybeDead(SymR->getSymbol());
1785
1786 SVal X = I.getData();
1787 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1788 for (; SI != SE; ++SI)
1789 SymReaper.maybeDead(*SI);
1790 }
1791
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001792 // Write the store back.
1793 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001794
1795 // Write the updated default bindings back.
1796 // FIXME: Right now this involves a fetching of a persistent state.
1797 // We can do better.
1798 if (DVM != NewDVM)
1799 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001800}
1801
1802//===----------------------------------------------------------------------===//
1803// Utility methods.
1804//===----------------------------------------------------------------------===//
1805
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001806void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001807 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001808 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001809 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001810
Ted Kremenek451ac092009-08-06 04:50:20 +00001811 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001812 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001813}