blob: 53ef054c5308ce86869e4aa8ba0066d1177411cb [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 Xu17892752008-10-08 02:50:44 +0000171public:
Ted Kremenek9af46f52009-06-16 22:36:44 +0000172 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000173 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000174 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000175 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000176
177 virtual ~RegionStoreManager() {}
178
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000179 SubRegionMap *getSubRegionMap(const GRState *state);
180
181 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000182
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000183
184 /// getDefaultBinding - Returns an SVal* representing an optional default
185 /// binding associated with a region and its subregions.
186 Optional<SVal> getDefaultBinding(const GRState *state, const MemRegion *R);
187
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000188 /// getLValueString - Returns an SVal representing the lvalue of a
189 /// StringLiteral. Within RegionStore a StringLiteral has an
190 /// associated StringRegion, and the lvalue of a StringLiteral is
191 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000192 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000193
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000194 /// getLValueCompoundLiteral - Returns an SVal representing the
195 /// lvalue of a compound literal. Within RegionStore a compound
196 /// literal has an associated region, and the lvalue of the
197 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000198 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000199
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000200 /// getLValueVar - Returns an SVal that represents the lvalue of a
201 /// variable. Within RegionStore a variable has an associated
202 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000203 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
204 const LocationContext *LC);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000205
Ted Kremenek67f28532009-06-17 22:02:04 +0000206 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000207
Ted Kremenek67f28532009-06-17 22:02:04 +0000208 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000209
Ted Kremenek67f28532009-06-17 22:02:04 +0000210 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000211
Ted Kremenek67f28532009-06-17 22:02:04 +0000212 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000213 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000214
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000215
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000216 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
217 /// type. 'Array' represents the lvalue of the array being decayed
218 /// to a pointer, and the returned SVal represents the decayed
219 /// version of that lvalue (i.e., a pointer to the first element of
220 /// the array). This is called by GRExprEngine when evaluating
221 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000222 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000223
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000224 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000225 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000226
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000227 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000228 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000229 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000230
Ted Kremenek67f28532009-06-17 22:02:04 +0000231 //===-------------------------------------------------------------------===//
232 // Binding values to regions.
233 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000234
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000235 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
236 const Expr *E, unsigned Count);
237
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000238private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000239 void RemoveSubRegionBindings(RegionBindings &B,
240 RegionDefaultBindings &DVM,
241 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000242 const MemRegion *R,
243 RegionStoreSubRegionMap &M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000244
245public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000246 const GRState *Bind(const GRState *state, Loc LV, SVal V);
247
248 const GRState *BindCompoundLiteral(const GRState *state,
249 const CompoundLiteralExpr* CL, SVal V);
250
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000251 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
252 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000253
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000254 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
255 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000256 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000257 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000258
Ted Kremenek67f28532009-06-17 22:02:04 +0000259 /// BindStruct - Bind a compound value to a structure.
260 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
261
262 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
263
264 /// KillStruct - Set the entire struct to unknown.
265 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
266
267 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
268
269 Store Remove(Store store, Loc LV);
270
271 //===------------------------------------------------------------------===//
272 // Loading values from regions.
273 //===------------------------------------------------------------------===//
274
275 /// The high level logic for this method is this:
276 /// Retrieve (L)
277 /// if L has binding
278 /// return L's binding
279 /// else if L is in killset
280 /// return unknown
281 /// else
282 /// if L is on stack or heap
283 /// return undefined
284 /// else
285 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000286 SValuator::CastResult Retrieve(const GRState *state, Loc L,
287 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000288
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000289 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000290
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000291 SVal RetrieveField(const GRState *state, const FieldRegion *R);
292
293 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Ted Kremenek25c54572009-07-20 22:58:02 +0000294
Ted Kremenek9031dd72009-07-21 00:12:07 +0000295 SVal RetrieveVar(const GRState *state, const VarRegion *R);
296
Ted Kremenek25c54572009-07-20 22:58:02 +0000297 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
298
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000299 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
300 QualType Ty, const MemRegion *superR);
301
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000302 SValuator::CastResult CastRetrievedVal(SVal val, const GRState *state,
303 const TypedRegion *R, QualType castTy);
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000304
Ted Kremenek67f28532009-06-17 22:02:04 +0000305 /// Retrieve the values in a struct and return a CompoundVal, used when doing
306 /// struct copy:
307 /// struct s x, y;
308 /// x = y;
309 /// y's value is retrieved by this method.
310 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
311
312 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000313
314 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000315 GetLazyBinding(RegionBindings B, const MemRegion *R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000316
317 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
318 const GRState *state,
319 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000320
321 //===------------------------------------------------------------------===//
322 // State pruning.
323 //===------------------------------------------------------------------===//
324
325 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
326 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000327 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000328 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
329
330 //===------------------------------------------------------------------===//
331 // Region "extents".
332 //===------------------------------------------------------------------===//
333
334 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
335 SVal getSizeInElements(const GRState *state, const MemRegion* R);
336
337 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000338 // Utility methods.
339 //===------------------------------------------------------------------===//
340
Ted Kremenek451ac092009-08-06 04:50:20 +0000341 static inline RegionBindings GetRegionBindings(Store store) {
342 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000343 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000344
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000345 void print(Store store, llvm::raw_ostream& Out, const char* nl,
346 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000347
348 void iterBindings(Store store, BindingsHandler& f) {
349 // FIXME: Implement.
350 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000351
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 // FIXME: Remove.
353 BasicValueFactory& getBasicVals() {
354 return StateMgr.getBasicVals();
355 }
356
357 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000358 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000359};
360
361} // end anonymous namespace
362
Ted Kremenek9af46f52009-06-16 22:36:44 +0000363//===----------------------------------------------------------------------===//
364// RegionStore creation.
365//===----------------------------------------------------------------------===//
366
367StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
368 RegionStoreFeatures F = maximal_features_tag();
369 return new RegionStoreManager(StMgr, F);
370}
371
372StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
373 RegionStoreFeatures F = minimal_features_tag();
374 F.enableFields(true);
375 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000376}
377
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000378void
379RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
380 const SubRegion *R) {
381 const MemRegion *superR = R->getSuperRegion();
382 if (add(superR, R))
383 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
384 WL.push_back(sr);
385}
386
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000387RegionStoreSubRegionMap*
388RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000389 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000390 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
391
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000392 llvm::SmallVector<const SubRegion*, 10> WL;
393
Ted Kremenek451ac092009-08-06 04:50:20 +0000394 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000395 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
396 M->process(WL, R);
397
Ted Kremenek451ac092009-08-06 04:50:20 +0000398 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
399 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000400 I != E; ++I)
401 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
402 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000403
404 // We also need to record in the subregion map "intermediate" regions that
405 // don't have direct bindings but are super regions of those that do.
406 while (!WL.empty()) {
407 const SubRegion *R = WL.back();
408 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000409 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000410 }
411
Ted Kremenek14453bf2009-03-03 19:02:42 +0000412 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000413}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000414
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000415SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
416 return getRegionStoreSubRegionMap(state);
417}
418
Ted Kremenek9af46f52009-06-16 22:36:44 +0000419//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000420// Binding invalidation.
421//===----------------------------------------------------------------------===//
422
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000423void
Ted Kremenek451ac092009-08-06 04:50:20 +0000424RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
425 RegionDefaultBindings &DVM,
426 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000427 const MemRegion *R,
428 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000429
430 RegionStoreSubRegionMap::iterator I, E;
431
432 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000433 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000434
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000435 B = RBFactory.Remove(B, R);
436 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437}
438
439
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000440const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
441 const MemRegion *R,
442 const Expr *E,
443 unsigned Count) {
444 ASTContext& Ctx = StateMgr.getContext();
445
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000446 // Strip away casts.
447 R = R->getBaseRegion();
448
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000449 // Remove the bindings to subregions.
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000450 {
451 // Get the mapping of regions -> subregions.
452 llvm::OwningPtr<RegionStoreSubRegionMap>
453 SubRegions(getRegionStoreSubRegionMap(state));
454
Ted Kremenek451ac092009-08-06 04:50:20 +0000455 RegionBindings B = GetRegionBindings(state->getStore());
456 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
457 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000458 state->get_context<RegionDefaultValue>();
459
460 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
461 state = state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
462 }
463
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000464 if (!R->isBoundable())
465 return state;
466
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000467 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
468 isa<ObjCObjectRegion>(R)) {
469 // Invalidate the region by setting its default value to
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000470 // conjured symbol. The type of the symbol is irrelavant.
471 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000472 return setDefaultValue(state, R, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000473 }
474
475 const TypedRegion *TR = cast<TypedRegion>(R);
476 QualType T = TR->getValueType(Ctx);
477
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000478 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000479 // FIXME: handle structs with default region value.
480 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
481
482 // No record definition. There is nothing we can do.
483 if (!RD)
484 return state;
485
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000486 // Invalidate the region by setting its default value to
487 // conjured symbol. The type of the symbol is irrelavant.
488 SVal V = ValMgr.getConjuredSymbolVal(E, Ctx.IntTy, Count);
489 return setDefaultValue(state, R, V);
490 }
491
492 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000493 // Set the default value of the array to conjured symbol.
494 SVal V = ValMgr.getConjuredSymbolVal(E, AT->getElementType(),
495 Count);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000496 return setDefaultValue(state, TR, V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000497 }
498
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000499 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
500 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
501 return Bind(state, ValMgr.makeLoc(TR), V);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000502}
503
504//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000505// getLValueXXX methods.
506//===----------------------------------------------------------------------===//
507
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000508/// getLValueString - Returns an SVal representing the lvalue of a
509/// StringLiteral. Within RegionStore a StringLiteral has an
510/// associated StringRegion, and the lvalue of a StringLiteral is the
511/// lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000512SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000513 const StringLiteral* S) {
514 return loc::MemRegionVal(MRMgr.getStringRegion(S));
515}
516
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000517/// getLValueVar - Returns an SVal that represents the lvalue of a
518/// variable. Within RegionStore a variable has an associated
519/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000520SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
521 const LocationContext *LC) {
522 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000523}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000524
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000525/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
526/// of a compound literal. Within RegionStore a compound literal
527/// has an associated region, and the lvalue of the compound literal
528/// is the lvalue of that region.
529SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000530RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000531 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000532 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
533}
534
Ted Kremenek67f28532009-06-17 22:02:04 +0000535SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000536 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000537 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000538}
539
Ted Kremenek67f28532009-06-17 22:02:04 +0000540SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000541 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000542 return getLValueFieldOrIvar(St, Base, D);
543}
544
Ted Kremenek67f28532009-06-17 22:02:04 +0000545SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000546 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000547 if (Base.isUnknownOrUndef())
548 return Base;
549
550 Loc BaseL = cast<Loc>(Base);
551 const MemRegion* BaseR = 0;
552
553 switch (BaseL.getSubKind()) {
554 case loc::MemRegionKind:
555 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
556 break;
557
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000558 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000559 // These are anormal cases. Flag an undefined value.
560 return UndefinedVal();
561
562 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000563 // While these seem funny, this can happen through casts.
564 // FIXME: What we should return is the field offset. For example,
565 // add the field offset to the integer value. That way funny things
566 // like this work properly: &(((struct foo *) 0xa)->f)
567 return Base;
568
569 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000570 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000571 return Base;
572 }
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000573
574 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
575 // of FieldDecl.
576 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
577 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000578
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000579 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000580}
581
Ted Kremenek67f28532009-06-17 22:02:04 +0000582SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000583 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000584 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000585
Ted Kremenekde7ec632009-03-09 22:44:49 +0000586 // If the base is an unknown or undefined value, just return it back.
587 // FIXME: For absolute pointer addresses, we just return that value back as
588 // well, although in reality we should return the offset added to that
589 // value.
590 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000591 return Base;
592
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000593 // Only handle integer offsets... for now.
594 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000595 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000596
Zhongxing Xuce760782009-05-09 13:20:07 +0000597 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000598
599 // Pointer of any type can be cast and used as array base.
600 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
601
Ted Kremenek46537392009-07-16 01:33:37 +0000602 // Convert the offset to the appropriate size and signedness.
603 Offset = ValMgr.convertToArrayIndex(Offset);
604
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000605 if (!ElemR) {
606 //
607 // If the base region is not an ElementRegion, create one.
608 // This can happen in the following example:
609 //
610 // char *p = __builtin_alloc(10);
611 // p[1] = 8;
612 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000613 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000614 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000615 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000616 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000617 }
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000618
619 SVal BaseIdx = ElemR->getIndex();
620
621 if (!isa<nonloc::ConcreteInt>(BaseIdx))
622 return UnknownVal();
623
624 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
625 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
626 assert(BaseIdxI.isSigned());
627
Ted Kremenek46537392009-07-16 01:33:37 +0000628 // Compute the new index.
629 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000630
Ted Kremenek46537392009-07-16 01:33:37 +0000631 // Construct the new ElementRegion.
632 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000633 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
634 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000635}
636
Ted Kremenek9af46f52009-06-16 22:36:44 +0000637//===----------------------------------------------------------------------===//
638// Extents for regions.
639//===----------------------------------------------------------------------===//
640
Ted Kremenek67f28532009-06-17 22:02:04 +0000641SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000642 const MemRegion *R) {
643
644 switch (R->getKind()) {
645 case MemRegion::MemSpaceRegionKind:
646 assert(0 && "Cannot index into a MemSpace");
647 return UnknownVal();
648
649 case MemRegion::CodeTextRegionKind:
650 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000651 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000652
653 // Not yet handled.
654 case MemRegion::AllocaRegionKind:
655 case MemRegion::CompoundLiteralRegionKind:
656 case MemRegion::ElementRegionKind:
657 case MemRegion::FieldRegionKind:
658 case MemRegion::ObjCIvarRegionKind:
659 case MemRegion::ObjCObjectRegionKind:
660 case MemRegion::SymbolicRegionKind:
661 return UnknownVal();
662
663 case MemRegion::StringRegionKind: {
664 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
665 // We intentionally made the size value signed because it participates in
666 // operations with signed indices.
667 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000668 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000669
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000670 case MemRegion::VarRegionKind: {
671 const VarRegion* VR = cast<VarRegion>(R);
672 // Get the type of the variable.
673 QualType T = VR->getDesugaredValueType(getContext());
674
675 // FIXME: Handle variable-length arrays.
676 if (isa<VariableArrayType>(T))
677 return UnknownVal();
678
679 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
680 // return the size as signed integer.
681 return ValMgr.makeIntVal(CAT->getSize(), false);
682 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000683
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000684 // Clients can use ordinary variables as if they were arrays. These
685 // essentially are arrays of size 1.
686 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000687 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000688
689 case MemRegion::BEG_DECL_REGIONS:
690 case MemRegion::END_DECL_REGIONS:
691 case MemRegion::BEG_TYPED_REGIONS:
692 case MemRegion::END_TYPED_REGIONS:
693 assert(0 && "Infeasible region");
694 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000695 }
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000696
697 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000698 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000699}
700
Ted Kremenek67f28532009-06-17 22:02:04 +0000701const GRState *RegionStoreManager::setExtent(const GRState *state,
702 const MemRegion *region,
703 SVal extent) {
704 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000705}
706
707//===----------------------------------------------------------------------===//
708// Location and region casting.
709//===----------------------------------------------------------------------===//
710
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000711/// ArrayToPointer - Emulates the "decay" of an array to a pointer
712/// type. 'Array' represents the lvalue of the array being decayed
713/// to a pointer, and the returned SVal represents the decayed
714/// version of that lvalue (i.e., a pointer to the first element of
715/// the array). This is called by GRExprEngine when evaluating casts
716/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000717SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000718 if (!isa<loc::MemRegionVal>(Array))
719 return UnknownVal();
720
721 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
722 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
723
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000724 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000725 return UnknownVal();
726
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000727 // Strip off typedefs from the ArrayRegion's ValueType.
728 QualType T = ArrayR->getValueType(getContext())->getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000729 ArrayType *AT = cast<ArrayType>(T);
730 T = AT->getElementType();
731
Ted Kremenek75185b52009-07-16 00:00:11 +0000732 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
733 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000734
735 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000736}
737
Ted Kremenek9af46f52009-06-16 22:36:44 +0000738//===----------------------------------------------------------------------===//
739// Pointer arithmetic.
740//===----------------------------------------------------------------------===//
741
Zhongxing Xu262fd032009-05-20 09:00:16 +0000742SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000743 BinaryOperator::Opcode Op, Loc L, NonLoc R,
744 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000745 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000746 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000747 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000748
Zhongxing Xua1718c72009-04-03 07:33:13 +0000749 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000750 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000751
Ted Kremenek3bccf082009-07-11 00:58:27 +0000752 switch (MR->getKind()) {
753 case MemRegion::SymbolicRegionKind: {
754 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000755 SymbolRef Sym = SR->getSymbol();
756 QualType T = Sym->getType(getContext());
Ted Kremenek6217b802009-07-29 21:53:49 +0000757 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000758 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
759 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
760 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000761 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000762 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000763 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000764 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000765 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000766 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
767 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
768 break;
769 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000770
Ted Kremenek3bccf082009-07-11 00:58:27 +0000771 case MemRegion::ElementRegionKind: {
772 ER = cast<ElementRegion>(MR);
773 break;
774 }
775
776 // Not yet handled.
777 case MemRegion::VarRegionKind:
778 case MemRegion::StringRegionKind:
779 case MemRegion::CompoundLiteralRegionKind:
780 case MemRegion::FieldRegionKind:
781 case MemRegion::ObjCObjectRegionKind:
782 case MemRegion::ObjCIvarRegionKind:
783 return UnknownVal();
784
Ted Kremenek3bccf082009-07-11 00:58:27 +0000785 case MemRegion::CodeTextRegionKind:
786 // Technically this can happen if people do funny things with casts.
787 return UnknownVal();
788
789 case MemRegion::MemSpaceRegionKind:
790 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
791 return UnknownVal();
792
793 case MemRegion::BEG_DECL_REGIONS:
794 case MemRegion::END_DECL_REGIONS:
795 case MemRegion::BEG_TYPED_REGIONS:
796 case MemRegion::END_TYPED_REGIONS:
797 assert(0 && "Infeasible region");
798 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000799 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000800
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000801 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000802 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
803 nonloc::ConcreteInt* Offset = dyn_cast<nonloc::ConcreteInt>(&R);
804
805 // Only support concrete integer indexes for now.
806 if (Base && Offset) {
Ted Kremenek46537392009-07-16 01:33:37 +0000807 // FIXME: Should use SValuator here.
808 SVal NewIdx = Base->evalBinOp(ValMgr, Op,
809 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekf936f452009-05-04 06:18:28 +0000810 const MemRegion* NewER =
Ted Kremenek46537392009-07-16 01:33:37 +0000811 MRMgr.getElementRegion(ER->getElementType(), NewIdx, ER->getSuperRegion(),
812 getContext());
Zhongxing Xud91ee272009-06-23 09:02:15 +0000813 return ValMgr.makeLoc(NewER);
Ted Kremenek5dc27462009-03-03 02:51:43 +0000814 }
815
816 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000817}
818
Ted Kremenek9af46f52009-06-16 22:36:44 +0000819//===----------------------------------------------------------------------===//
820// Loading values from regions.
821//===----------------------------------------------------------------------===//
822
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000823Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
824 const MemRegion *R) {
825
826 if (R->isBoundable())
827 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
828 if (TR->getValueType(getContext())->isUnionType())
829 return UnknownVal();
830
831 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
832}
833
Ted Kremeneka6275a52009-07-15 02:31:43 +0000834static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
835 RTy = Ctx.getCanonicalType(RTy);
836 UsedTy = Ctx.getCanonicalType(UsedTy);
837
838 if (RTy == UsedTy)
839 return false;
840
Ted Kremenek25c54572009-07-20 22:58:02 +0000841
842 // Recursively check the types. We basically want to see if a pointer value
843 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
844 // represents a reinterpretation.
845 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000846 const PointerType *PRTy = RTy->getAs<PointerType>();
847 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000848
849 return PUsedTy && PRTy &&
850 IsReinterpreted(PRTy->getPointeeType(),
851 PUsedTy->getPointeeType(), Ctx);
852 }
853
854 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000855}
856
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000857SValuator::CastResult
858RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000859
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000860 assert(!isa<UnknownVal>(L) && "location unknown");
861 assert(!isa<UndefinedVal>(L) && "location undefined");
862
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000863 // FIXME: Is this even possible? Shouldn't this be treated as a null
864 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000865 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000866 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000867
Ted Kremenek67f28532009-06-17 22:02:04 +0000868 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000869
Zhongxing Xu91844122009-05-20 09:18:48 +0000870 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000871 // Example:
872 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000873 // char* p = alloca();
874 // read(p);
875 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000876 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000877 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000878
879 if (isa<SymbolicRegion>(MR)) {
880 ASTContext &Ctx = getContext();
Zhongxing Xud79bf552009-07-15 05:09:24 +0000881 SVal idx = ValMgr.makeZeroArrayIndex();
Ted Kremeneka6275a52009-07-15 02:31:43 +0000882 assert(!T.isNull());
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000883 MR = MRMgr.getElementRegion(T, idx, MR, Ctx);
884 }
885
Ted Kremenek968f0a62009-08-03 21:41:46 +0000886 if (isa<CodeTextRegion>(MR))
887 return SValuator::CastResult(state, UnknownVal());
888
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000889 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
890 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000891 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000892 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000893
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000894 // FIXME: We should eventually handle funny addressing. e.g.:
895 //
896 // int x = ...;
897 // int *p = &x;
898 // char *q = (char*) p;
899 // char c = *q; // returns the first byte of 'x'.
900 //
901 // Such funny addressing will occur due to layering of regions.
902
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000903#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000904 ASTContext &Ctx = getContext();
905 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000906 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
907 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000908 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000909 assert(Ctx.getCanonicalType(RTy) ==
910 Ctx.getCanonicalType(R->getValueType(Ctx)));
Ted Kremeneka6275a52009-07-15 02:31:43 +0000911 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000912#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000913
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000914 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000915 return SValuator::CastResult(state, RetrieveStruct(state, R));
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000916
917 // FIXME: Handle unions.
918 if (RTy->isUnionType())
919 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000920
921 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000922 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000923
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000924 // FIXME: handle Vector types.
925 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000926 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000927
928 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000929 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +0000930
931 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000932 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000933
Ted Kremenek25c54572009-07-20 22:58:02 +0000934 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000935 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Ted Kremenek9031dd72009-07-21 00:12:07 +0000936
937 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000938 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +0000939
Ted Kremenek451ac092009-08-06 04:50:20 +0000940 RegionBindings B = GetRegionBindings(state->getStore());
941 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000942
943 // Check if the region has a binding.
944 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000945 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000946
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000947 // The location does not have a bound value. This means that it has
948 // the value it had upon its creation and/or entry to the analyzed
949 // function/method. These are either symbolic values or 'undefined'.
950
Ted Kremenek356e9d62009-07-22 04:35:42 +0000951#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +0000952 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +0000953#else
954 if (R->hasStackStorage()) {
955#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000956 // All stack variables are considered to have undefined values
957 // upon creation. All heap allocated blocks are considered to
958 // have undefined values as well unless they are explicitly bound
959 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000960 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000961 }
962
Ted Kremenekbb2b4332009-07-02 22:16:42 +0000963 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000964 return SValuator::CastResult(state,
965 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000966}
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000967
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000968std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000969RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000970
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000971 if (const nonloc::LazyCompoundVal *V =
972 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
973 return std::make_pair(V->getState(), V->getRegion());
974
975 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
976 const std::pair<const GRState *, const MemRegion *> &X =
977 GetLazyBinding(B, ER->getSuperRegion());
978
979 if (X.first)
980 return std::make_pair(X.first,
981 MRMgr.getElementRegionWithSuper(ER, X.second));
982 }
983 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
984 const std::pair<const GRState *, const MemRegion *> &X =
985 GetLazyBinding(B, FR->getSuperRegion());
986
987 if (X.first)
988 return std::make_pair(X.first,
989 MRMgr.getFieldRegionWithSuper(FR, X.second));
990 }
991
992 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
993}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000994
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000995SVal RegionStoreManager::RetrieveElement(const GRState* state,
996 const ElementRegion* R) {
997 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +0000998 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +0000999 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001000 return *V;
1001
Ted Kremenek921109a2009-07-01 23:19:52 +00001002 const MemRegion* superR = R->getSuperRegion();
1003
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001004 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001005 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001006 const StringLiteral *Str = StrR->getStringLiteral();
1007 SVal Idx = R->getIndex();
1008 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1009 int64_t i = CI->getValue().getSExtValue();
1010 char c;
1011 if (i == Str->getByteLength())
1012 c = '\0';
1013 else
1014 c = Str->getStrData()[i];
1015 return ValMgr.makeIntVal(c, getContext().CharTy);
1016 }
1017 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001018
1019 // Special case: the current region represents a cast and it and the super
1020 // region both have pointer types or intptr_t types. If so, perform the
1021 // retrieve from the super region and appropriately "cast" the value.
1022 // This is needed to support OSAtomicCompareAndSwap and friends or other
1023 // loads that treat integers as pointers and vis versa.
1024 if (R->getIndex().isZeroConstant()) {
1025 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1026 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001027 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1028 QualType valTy = R->getValueType(Ctx);
1029 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1030 // Retrieve the value from the super region. This will be casted to
1031 // valTy when we return to 'Retrieve'.
1032 const SValuator::CastResult &cr = Retrieve(state,
1033 loc::MemRegionVal(superR),
1034 valTy);
1035 return cr.getSVal();
1036 }
1037 }
1038 }
1039 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001040
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001041 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001042 if (const SVal *V = B.lookup(superR)) {
1043 if (SymbolRef parentSym = V->getAsSymbol())
1044 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001045
1046 if (V->isUnknownOrUndef())
1047 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001048
1049 // Handle LazyCompoundVals for the immediate super region. Other cases
1050 // are handled in 'RetrieveFieldOrElementCommon'.
1051 if (const nonloc::LazyCompoundVal *LCV =
1052 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001053
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001054 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1055 return RetrieveElement(LCV->getState(), R);
1056 }
1057
Ted Kremeneka6275a52009-07-15 02:31:43 +00001058 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001059 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001060 }
Ted Kremenek921109a2009-07-01 23:19:52 +00001061
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001062 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001063}
1064
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001065SVal RegionStoreManager::RetrieveField(const GRState* state,
1066 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001067
1068 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001069 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001070 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001071 return *V;
1072
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001073 QualType Ty = R->getValueType(getContext());
1074 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1075}
1076
1077SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1078 const TypedRegion *R,
1079 QualType Ty,
1080 const MemRegion *superR) {
1081
1082 // At this point we have already checked in either RetrieveElement or
1083 // RetrieveField if 'R' has a direct binding.
1084
1085 RegionBindings B = GetRegionBindings(state->getStore());
1086
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001087 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001088 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001089 if (SymbolRef parentSym = D->getAsSymbol())
1090 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001091
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001092 if (D->isZeroConstant())
1093 return ValMgr.makeZeroVal(Ty);
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001094
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001095 if (D->isUnknown())
1096 return *D;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001097
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001098 assert(0 && "Unknown default value");
1099 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001100
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001101 // If our super region is a field or element itself, walk up the region
1102 // hierarchy to see if there is a default value installed in an ancestor.
1103 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1104 superR = cast<SubRegion>(superR)->getSuperRegion();
1105 continue;
1106 }
1107
1108 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001109 }
1110
1111 // Lazy binding?
1112 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001113 const MemRegion *lazyBindingRegion = NULL;
1114 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001115
1116 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001117 assert(lazyBindingRegion && "Lazy-binding region not set");
1118
1119 if (isa<ElementRegion>(R))
1120 return RetrieveElement(lazyBindingState,
1121 cast<ElementRegion>(lazyBindingRegion));
1122
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001123 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001124 cast<FieldRegion>(lazyBindingRegion));
1125 }
Ted Kremenekdc147262009-07-02 22:02:15 +00001126
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001127 if (R->hasStackStorage() && !R->hasParametersStorage()) {
1128
1129 if (isa<ElementRegion>(R)) {
1130 // Currently we don't reason specially about Clang-style vectors. Check
1131 // if superR is a vector and if so return Unknown.
1132 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1133 if (typedSuperR->getValueType(getContext())->isVectorType())
1134 return UnknownVal();
1135 }
1136 }
1137
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001138 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001139 }
1140
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001141 // All other values are symbolic.
1142 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001143}
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001144
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001145SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
1146 const ObjCIvarRegion* R) {
1147
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001148 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001149 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001150
1151 if (const SVal* V = B.lookup(R))
1152 return *V;
1153
1154 const MemRegion *superR = R->getSuperRegion();
1155
1156 // Check if the super region has a binding.
1157 if (const SVal *V = B.lookup(superR)) {
1158 if (SymbolRef parentSym = V->getAsSymbol())
1159 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
1160
1161 // Other cases: give up.
1162 return UnknownVal();
1163 }
1164
Ted Kremenek25c54572009-07-20 22:58:02 +00001165 return RetrieveLazySymbol(state, R);
1166}
1167
Ted Kremenek9031dd72009-07-21 00:12:07 +00001168SVal RegionStoreManager::RetrieveVar(const GRState *state,
1169 const VarRegion *R) {
1170
1171 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001172 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek9031dd72009-07-21 00:12:07 +00001173
1174 if (const SVal* V = B.lookup(R))
1175 return *V;
1176
1177 // Lazily derive a value for the VarRegion.
1178 const VarDecl *VD = R->getDecl();
1179
Ted Kremenek9031dd72009-07-21 00:12:07 +00001180 if (R->hasGlobalsOrParametersStorage())
1181 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
1182
1183 return UndefinedVal();
1184}
1185
Ted Kremenek25c54572009-07-20 22:58:02 +00001186SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
1187 const TypedRegion *R) {
1188
1189 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001190
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001191 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001192 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001193}
1194
Zhongxing Xu88c675f2009-06-18 06:29:10 +00001195SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1196 const TypedRegion* R){
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001197 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001198 assert(T->isStructureType());
1199
Zhongxing Xub7507d12009-06-11 07:27:30 +00001200 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001201 RecordDecl* RD = RT->getDecl();
1202 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001203 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001204#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001205 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1206
Ted Kremenek67f28532009-06-17 22:02:04 +00001207 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1208 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001209 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001210
Douglas Gregore267ff32008-12-11 20:41:00 +00001211 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1212 FieldEnd = Fields.rend();
1213 Field != FieldEnd; ++Field) {
1214 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001215 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001216 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001217 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1218 }
1219
Zhongxing Xud91ee272009-06-23 09:02:15 +00001220 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001221#else
1222 return ValMgr.makeLazyCompoundVal(state, R);
1223#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001224}
1225
Ted Kremenek67f28532009-06-17 22:02:04 +00001226SVal RegionStoreManager::RetrieveArray(const GRState *state,
1227 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001228#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001229 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001230 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1231
1232 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001233 uint64_t size = CAT->getSize().getZExtValue();
1234 for (uint64_t i = 0; i < size; ++i) {
1235 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001236 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
1237 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001238 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001239 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001240 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1241 }
1242
Zhongxing Xud91ee272009-06-23 09:02:15 +00001243 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001244#else
1245 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1246 return ValMgr.makeLazyCompoundVal(state, R);
1247#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001248}
1249
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001250SValuator::CastResult RegionStoreManager::CastRetrievedVal(SVal V,
1251 const GRState *state,
1252 const TypedRegion *R,
1253 QualType castTy) {
Ted Kremenek9031dd72009-07-21 00:12:07 +00001254 if (castTy.isNull())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001255 return SValuator::CastResult(state, V);
Ted Kremenek9031dd72009-07-21 00:12:07 +00001256
1257 ASTContext &Ctx = getContext();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001258 return ValMgr.getSValuator().EvalCast(V, state, castTy, R->getValueType(Ctx));
Ted Kremenek25c54572009-07-20 22:58:02 +00001259}
1260
Ted Kremenek9af46f52009-06-16 22:36:44 +00001261//===----------------------------------------------------------------------===//
1262// Binding values to regions.
1263//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001264
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001265Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001266 const MemRegion* R = 0;
1267
1268 if (isa<loc::MemRegionVal>(L))
1269 R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek0964a062009-01-21 06:57:53 +00001270
1271 if (R) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001272 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001273 return RBFactory.Remove(B, R).getRoot();
1274 }
1275
1276 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001277}
1278
Ted Kremenek67f28532009-06-17 22:02:04 +00001279const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001280 if (isa<loc::ConcreteInt>(L))
1281 return state;
1282
Ted Kremenek9af46f52009-06-16 22:36:44 +00001283 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001284 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001285
1286 // Check if the region is a struct region.
1287 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1288 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001289 return BindStruct(state, TR, V);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001290
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001291 // Special case: the current region represents a cast and it and the super
1292 // region both have pointer types or intptr_t types. If so, perform the
1293 // bind to the super region.
1294 // This is needed to support OSAtomicCompareAndSwap and friends or other
1295 // loads that treat integers as pointers and vis versa.
1296 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1297 if (ER->getIndex().isZeroConstant()) {
1298 if (const TypedRegion *superR =
1299 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1300 ASTContext &Ctx = getContext();
1301 QualType superTy = superR->getValueType(Ctx);
1302 QualType erTy = ER->getValueType(Ctx);
1303
1304 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
1305 IsAnyPointerOrIntptr(erTy, Ctx)) {
1306 SValuator::CastResult cr =
1307 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
1308 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1309 }
1310 }
1311 }
1312 }
1313
1314 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001315 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001316 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001317}
1318
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001319const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1320 const VarDecl *VD,
1321 const LocationContext *LC,
1322 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001323
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001324 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001325 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001326
Ted Kremenek0964a062009-01-21 06:57:53 +00001327 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001328 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001329 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001330 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001331
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001332 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001333}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001334
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001335// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001336const GRState *
1337RegionStoreManager::BindCompoundLiteral(const GRState *state,
1338 const CompoundLiteralExpr* CL,
1339 SVal V) {
1340
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001341 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001342 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001343}
1344
Ted Kremenek67f28532009-06-17 22:02:04 +00001345const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001346 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001347 SVal Init) {
1348
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001349 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001350 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001351 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001352
Ted Kremenek46537392009-07-16 01:33:37 +00001353 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001354
1355 // Check if the init expr is a StringLiteral.
1356 if (isa<loc::MemRegionVal>(Init)) {
1357 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1358 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1359 const char* str = S->getStrData();
1360 unsigned len = S->getByteLength();
1361 unsigned j = 0;
1362
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001363 // Copy bytes from the string literal into the target array. Trailing bytes
1364 // in the array that are not covered by the string literal are initialized
1365 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001366 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001367 if (j >= len)
1368 break;
1369
Ted Kremenek46537392009-07-16 01:33:37 +00001370 SVal Idx = ValMgr.makeArrayIndex(i);
1371 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1372 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001373
Zhongxing Xud91ee272009-06-23 09:02:15 +00001374 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001375 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001376 }
1377
Ted Kremenek67f28532009-06-17 22:02:04 +00001378 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001379 }
1380
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001381 // Handle lazy compound values.
1382 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1383 return CopyLazyBindings(*LCV, state, R);
1384
1385 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001386 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001387 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001388 uint64_t i = 0;
1389
1390 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001391 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001392 if (VI == VE)
1393 break;
1394
Ted Kremenek46537392009-07-16 01:33:37 +00001395 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001396 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001397
1398 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001399 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001400 else
Zhongxing Xud91ee272009-06-23 09:02:15 +00001401 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001402 }
1403
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001404 // If the init list is shorter than the array length, set the array default
1405 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001406 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001407 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001408 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001409 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001410 }
1411 }
1412
Ted Kremenek67f28532009-06-17 22:02:04 +00001413 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001414}
1415
Ted Kremenek67f28532009-06-17 22:02:04 +00001416const GRState *
1417RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1418 SVal V) {
1419
1420 if (!Features.supportsFields())
1421 return state;
1422
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001423 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001424 assert(T->isStructureType());
1425
Ted Kremenek6217b802009-07-29 21:53:49 +00001426 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001427 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001428
1429 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001430 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001431
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001432 // Handle lazy compound values.
1433 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1434 return CopyLazyBindings(*LCV, state, R);
1435
Ted Kremenek67f28532009-06-17 22:02:04 +00001436 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1437 // Ignore them and kill the field values.
1438 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1439 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001440
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001441 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001442 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001443
1444 RecordDecl::field_iterator FI, FE;
1445
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001446 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001447
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001448 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001449 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001450
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001451 QualType FTy = (*FI)->getType();
1452 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
1453
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001454 if (Loc::IsLocType(FTy) || FTy->isIntegerType())
Zhongxing Xud91ee272009-06-23 09:02:15 +00001455 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001456 else if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001457 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001458 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001459 state = BindStruct(state, FR, *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001460 }
1461
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001462 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001463 if (FI != FE)
1464 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001465
Ted Kremenek67f28532009-06-17 22:02:04 +00001466 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001467}
1468
Ted Kremenek67f28532009-06-17 22:02:04 +00001469const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001470 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001471
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001472 // Set the default value of the struct region to "unknown".
1473 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001474
1475 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001476 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001477 RegionBindings B = GetRegionBindings(store);
1478 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001479 const MemRegion* R = I.getKey();
1480 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1481 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001482 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001483 }
1484
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001486}
1487
Ted Kremenek67f28532009-06-17 22:02:04 +00001488const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1489 const MemRegion* R, SVal V) {
1490 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001491}
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001492
1493const GRState*
1494RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1495 const GRState *state,
1496 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001497
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001498 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001499 RegionBindings B = GetRegionBindings(state->getStore());
1500 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1501 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001502 state->get_context<RegionDefaultValue>();
1503
1504 llvm::OwningPtr<RegionStoreSubRegionMap>
1505 SubRegions(getRegionStoreSubRegionMap(state));
1506
1507 // B and DVM are updated after the call to RemoveSubRegionBindings.
1508 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
1509
1510 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1511 // results in a zero-copy algorithm.
1512 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1513}
1514
Ted Kremenek9af46f52009-06-16 22:36:44 +00001515//===----------------------------------------------------------------------===//
1516// State pruning.
1517//===----------------------------------------------------------------------===//
Ted Kremenek451ac092009-08-06 04:50:20 +00001518
Ted Kremenek9af46f52009-06-16 22:36:44 +00001519static void UpdateLiveSymbols(SVal X, SymbolReaper& SymReaper) {
1520 if (loc::MemRegionVal *XR = dyn_cast<loc::MemRegionVal>(&X)) {
1521 const MemRegion *R = XR->getRegion();
1522
1523 while (R) {
1524 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1525 SymReaper.markLive(SR->getSymbol());
1526 return;
1527 }
1528
1529 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1530 R = SR->getSuperRegion();
1531 continue;
1532 }
1533
1534 break;
1535 }
1536
1537 return;
1538 }
1539
1540 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();SI!=SE;++SI)
1541 SymReaper.markLive(*SI);
1542}
Ted Kremenek451ac092009-08-06 04:50:20 +00001543
1544namespace {
1545class VISIBILITY_HIDDEN TreeScanner {
1546 RegionBindings B;
1547 RegionDefaultBindings DB;
1548 SymbolReaper &SymReaper;
1549 llvm::DenseSet<const MemRegion*> &Marked;
1550 llvm::DenseSet<const LazyCompoundValData*> &ScannedLazyVals;
1551 RegionStoreSubRegionMap &M;
1552 RegionStoreManager &RS;
1553 llvm::SmallVectorImpl<const MemRegion*> &RegionRoots;
1554 const bool MarkKeys;
1555public:
1556 TreeScanner(RegionBindings b, RegionDefaultBindings db,
1557 SymbolReaper &symReaper,
1558 llvm::DenseSet<const MemRegion*> &marked,
1559 llvm::DenseSet<const LazyCompoundValData*> &scannedLazyVals,
1560 RegionStoreSubRegionMap &m, RegionStoreManager &rs,
1561 llvm::SmallVectorImpl<const MemRegion*> &regionRoots,
1562 bool markKeys = true)
1563 : B(b), DB(db), SymReaper(symReaper), Marked(marked),
1564 ScannedLazyVals(scannedLazyVals), M(m),
1565 RS(rs), RegionRoots(regionRoots), MarkKeys(markKeys) {}
1566
1567 void scanTree(const MemRegion *R);
1568};
1569} // end anonymous namespace
1570
1571
1572void TreeScanner::scanTree(const MemRegion *R) {
1573 if (MarkKeys) {
1574 if (Marked.count(R))
1575 return;
1576
1577 Marked.insert(R);
1578 }
1579
1580 // Mark the symbol for any live SymbolicRegion as "live". This means we
1581 // should continue to track that symbol.
1582 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1583 SymReaper.markLive(SymR->getSymbol());
1584
1585 // Get the data binding for R (if any).
1586 const SVal* Xptr = B.lookup(R);
1587
1588 // Check for lazy bindings.
1589 if (const nonloc::LazyCompoundVal *V =
1590 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1591
1592 const LazyCompoundValData *D = V->getCVData();
1593
1594 if (!ScannedLazyVals.count(D)) {
1595 // Scan the bindings in the LazyCompoundVal.
1596 ScannedLazyVals.insert(D);
1597
1598 // FIXME: Cache subregion maps.
1599 const GRState *lazyState = D->getState();
1600
1601 llvm::OwningPtr<RegionStoreSubRegionMap>
1602 lazySM(RS.getRegionStoreSubRegionMap(lazyState));
1603
1604 Store lazyStore = lazyState->getStore();
1605 RegionBindings lazyB = RS.GetRegionBindings(lazyStore);
1606
1607 RegionDefaultBindings lazyDB = lazyState->get<RegionDefaultValue>();
1608
1609 // Scan the bindings.
1610 TreeScanner scan(lazyB, lazyDB, SymReaper, Marked, ScannedLazyVals,
1611 *lazySM.get(), RS, RegionRoots, false);
1612
1613 scan.scanTree(D->getRegion());
1614 }
1615 }
1616 else {
1617 // No direct binding? Get the default binding for R (if any).
1618 if (!Xptr)
1619 Xptr = DB.lookup(R);
1620
1621 // Direct or default binding?
1622 if (Xptr) {
1623 SVal X = *Xptr;
1624 UpdateLiveSymbols(X, SymReaper); // Update the set of live symbols.
1625
1626 // If X is a region, then add it to the RegionRoots.
1627 if (const MemRegion *RX = X.getAsRegion()) {
1628 RegionRoots.push_back(RX);
1629 // Mark the super region of the RX as live.
1630 // e.g.: int x; char *y = (char*) &x; if (*y) ...
1631 // 'y' => element region. 'x' is its super region.
1632 if (const SubRegion *SR = dyn_cast<SubRegion>(RX)) {
1633 RegionRoots.push_back(SR->getSuperRegion());
1634 }
1635 }
1636 }
1637 }
1638
1639 RegionStoreSubRegionMap::iterator I, E;
1640
1641 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
1642 scanTree(*I);
1643}
Ted Kremenek9af46f52009-06-16 22:36:44 +00001644
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001645void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
1646 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001647 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Ted Kremenek67f28532009-06-17 22:02:04 +00001648{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001649 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001650 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001651
1652 // Lazily constructed backmap from MemRegions to SubRegions.
1653 typedef llvm::ImmutableSet<const MemRegion*> SubRegionsTy;
1654 typedef llvm::ImmutableMap<const MemRegion*, SubRegionsTy> SubRegionsMapTy;
1655
Ted Kremenek9af46f52009-06-16 22:36:44 +00001656 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001657 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001658 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001659
1660 // Do a pass over the regions in the store. For VarRegions we check if
1661 // the variable is still live and if so add it to the list of live roots.
Ted Kremenek67f28532009-06-17 22:02:04 +00001662 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001663 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
1664
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001665 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001666 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001667 const MemRegion *R = I.getKey();
1668 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 }
1670
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001671 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001672 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
1673 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001674 I != E; ++I) {
1675 const MemRegion *R = I.getKey();
1676 IntermediateRoots.push_back(R);
1677 }
1678
1679 // Process the "intermediate" roots to find if they are referenced by
1680 // real roots.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001681 while (!IntermediateRoots.empty()) {
1682 const MemRegion* R = IntermediateRoots.back();
1683 IntermediateRoots.pop_back();
1684
1685 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001686 if (SymReaper.isLive(Loc, VR->getDecl())) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001687 RegionRoots.push_back(VR); // This is a live "root".
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001688 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001689 continue;
1690 }
1691
1692 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001693 if (SymReaper.isLive(SR->getSymbol()))
1694 RegionRoots.push_back(SR);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001695 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001696 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001697
1698 // Add the super region for R to the worklist if it is a subregion.
1699 if (const SubRegion* superR =
1700 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
1701 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001702 }
1703
1704 // Process the worklist of RegionRoots. This performs a "mark-and-sweep"
1705 // of the store. We want to find all live symbols and dead regions.
Ted Kremenek451ac092009-08-06 04:50:20 +00001706 llvm::DenseSet<const MemRegion*> Marked;
1707 llvm::DenseSet<const LazyCompoundValData*> LazyVals;
1708 TreeScanner TS(B, DVM, SymReaper, Marked, LazyVals, *SubRegions.get(),
1709 *this, RegionRoots);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001710
Ted Kremenek451ac092009-08-06 04:50:20 +00001711 while (!RegionRoots.empty()) {
1712 const MemRegion *R = RegionRoots.back();
1713 RegionRoots.pop_back();
1714 TS.scanTree(R);
1715 }
Ted Kremenek9af46f52009-06-16 22:36:44 +00001716
Ted Kremenek9af46f52009-06-16 22:36:44 +00001717 // We have now scanned the store, marking reachable regions and symbols
1718 // as live. We now remove all the regions that are dead from the store
1719 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001720 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001721 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001722 // If this region live? Is so, none of its symbols are dead.
1723 if (Marked.count(R))
1724 continue;
1725
1726 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001727 store = Remove(store, ValMgr.makeLoc(R));
Ted Kremenek9af46f52009-06-16 22:36:44 +00001728
1729 // Mark all non-live symbols that this region references as dead.
1730 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1731 SymReaper.maybeDead(SymR->getSymbol());
1732
1733 SVal X = I.getData();
1734 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001735 for (; SI != SE; ++SI)
1736 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001737 }
1738
Ted Kremenek093569c2009-08-02 05:00:15 +00001739 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001740 RegionDefaultBindings NewDVM = DVM;
1741 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001742 state.get_context<RegionDefaultValue>();
1743
Ted Kremenek451ac092009-08-06 04:50:20 +00001744 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001745 I != E; ++I) {
1746 const MemRegion *R = I.getKey();
1747
1748 // If this region live? Is so, none of its symbols are dead.
1749 if (Marked.count(R))
1750 continue;
1751
1752 // Remove this dead region.
1753 NewDVM = DVMFactory.Remove(NewDVM, R);
1754
1755 // Mark all non-live symbols that this region references as dead.
1756 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1757 SymReaper.maybeDead(SymR->getSymbol());
1758
1759 SVal X = I.getData();
1760 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1761 for (; SI != SE; ++SI)
1762 SymReaper.maybeDead(*SI);
1763 }
1764
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001765 // Write the store back.
1766 state.setStore(store);
Ted Kremenek093569c2009-08-02 05:00:15 +00001767
1768 // Write the updated default bindings back.
1769 // FIXME: Right now this involves a fetching of a persistent state.
1770 // We can do better.
1771 if (DVM != NewDVM)
1772 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001773}
1774
1775//===----------------------------------------------------------------------===//
1776// Utility methods.
1777//===----------------------------------------------------------------------===//
1778
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001779void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001780 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001781 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001782 OS << "Store (direct bindings):" << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001783
Ted Kremenek451ac092009-08-06 04:50:20 +00001784 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001785 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001786}