blob: 46e1d12a3cbc0ec5334f88f49a691dfaac589d25 [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 {};
Mike Stump1eb44332009-09-09 15:08:12 +000044struct VISIBILITY_HIDDEN maximal_features_tag {};
45
Ted Kremenek9af46f52009-06-16 22:36:44 +000046class VISIBILITY_HIDDEN RegionStoreFeatures {
47 bool SupportsFields;
48 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenek9af46f52009-06-16 22:36:44 +000050public:
51 RegionStoreFeatures(minimal_features_tag) :
52 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek9af46f52009-06-16 22:36:44 +000054 RegionStoreFeatures(maximal_features_tag) :
55 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000056
Ted Kremenek9af46f52009-06-16 22:36:44 +000057 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenek9af46f52009-06-16 22:36:44 +000059 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000111 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 {
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000121class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
122 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000123 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000124 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000139 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek59e8f112009-03-03 01:35:36 +0000141 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000142
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;
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek5dc27462009-03-03 02:51:43 +0000156 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000157 }
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000159 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167
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;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000171
172 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
173 SMCache SC;
174
Zhongxing Xu17892752008-10-08 02:50:44 +0000175public:
Mike Stump1eb44332009-09-09 15:08:12 +0000176 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000177 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000178 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000179 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000180
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000181 virtual ~RegionStoreManager() {
182 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
183 delete (*I).second;
184 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000185
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000186 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000188 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000189
190
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000191 /// getDefaultBinding - Returns an SVal* representing an optional default
192 /// binding associated with a region and its subregions.
193 Optional<SVal> getDefaultBinding(const GRState *state, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000195 /// getLValueString - Returns an SVal representing the lvalue of a
196 /// StringLiteral. Within RegionStore a StringLiteral has an
197 /// associated StringRegion, and the lvalue of a StringLiteral is
198 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000199 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000200
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000201 /// getLValueCompoundLiteral - Returns an SVal representing the
202 /// lvalue of a compound literal. Within RegionStore a compound
203 /// literal has an associated region, and the lvalue of the
204 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000205 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000206
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000207 /// getLValueVar - Returns an SVal that represents the lvalue of a
208 /// variable. Within RegionStore a variable has an associated
209 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000210 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
211 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenek67f28532009-06-17 22:02:04 +0000213 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000214
Ted Kremenek67f28532009-06-17 22:02:04 +0000215 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Ted Kremenek67f28532009-06-17 22:02:04 +0000217 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000218
Ted Kremenek67f28532009-06-17 22:02:04 +0000219 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000220 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000221
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000222
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000223 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
224 /// type. 'Array' represents the lvalue of the array being decayed
225 /// to a pointer, and the returned SVal represents the decayed
226 /// version of that lvalue (i.e., a pointer to the first element of
227 /// the array). This is called by GRExprEngine when evaluating
228 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000229 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000230
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000231 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000232 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000233
Mike Stump1eb44332009-09-09 15:08:12 +0000234 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000235 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000236 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000237
Ted Kremenek67f28532009-06-17 22:02:04 +0000238 //===-------------------------------------------------------------------===//
239 // Binding values to regions.
240 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000241
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000242 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
243 const Expr *E, unsigned Count);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000245private:
Ted Kremenek451ac092009-08-06 04:50:20 +0000246 void RemoveSubRegionBindings(RegionBindings &B,
247 RegionDefaultBindings &DVM,
248 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000249 const MemRegion *R,
250 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000251
252public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000253 const GRState *Bind(const GRState *state, Loc LV, SVal V);
254
255 const GRState *BindCompoundLiteral(const GRState *state,
256 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000258 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
259 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000260
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000261 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
262 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000263 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000264 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 /// BindStruct - Bind a compound value to a structure.
267 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek67f28532009-06-17 22:02:04 +0000269 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
271 /// KillStruct - Set the entire struct to unknown.
Ted Kremenek67f28532009-06-17 22:02:04 +0000272 const GRState *KillStruct(const GRState *state, const TypedRegion* R);
273
274 const GRState *setDefaultValue(const GRState *state, const MemRegion* R, SVal V);
275
276 Store Remove(Store store, Loc LV);
277
278 //===------------------------------------------------------------------===//
279 // Loading values from regions.
280 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenek67f28532009-06-17 22:02:04 +0000282 /// The high level logic for this method is this:
283 /// Retrieve (L)
284 /// if L has binding
285 /// return L's binding
286 /// else if L is in killset
287 /// return unknown
288 /// else
289 /// if L is on stack or heap
290 /// return undefined
291 /// else
292 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000293 SValuator::CastResult Retrieve(const GRState *state, Loc L,
294 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000295
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000296 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000297
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000298 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000300 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Ted Kremenek9031dd72009-07-21 00:12:07 +0000302 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Ted Kremenek25c54572009-07-20 22:58:02 +0000304 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000306 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
307 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
Ted Kremenek67f28532009-06-17 22:02:04 +0000309 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000310 /// struct copy:
311 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000312 /// x = y;
313 /// y's value is retrieved by this method.
314 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek67f28532009-06-17 22:02:04 +0000316 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000318 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000319 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000321 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
322 const GRState *state,
323 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000324
Ted Kremenek0954cde2009-09-24 04:11:44 +0000325 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
326 QualType T);
327
Ted Kremenek67f28532009-06-17 22:02:04 +0000328 //===------------------------------------------------------------------===//
329 // State pruning.
330 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek67f28532009-06-17 22:02:04 +0000332 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
333 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000334 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000335 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
336
337 //===------------------------------------------------------------------===//
338 // Region "extents".
339 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
342 SVal getSizeInElements(const GRState *state, const MemRegion* R);
343
344 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000345 // Utility methods.
346 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek451ac092009-08-06 04:50:20 +0000348 static inline RegionBindings GetRegionBindings(Store store) {
349 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000350 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000351
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000352 void print(Store store, llvm::raw_ostream& Out, const char* nl,
353 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000354
355 void iterBindings(Store store, BindingsHandler& f) {
356 // FIXME: Implement.
357 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000358
Ted Kremenek67f28532009-06-17 22:02:04 +0000359 // FIXME: Remove.
360 BasicValueFactory& getBasicVals() {
361 return StateMgr.getBasicVals();
362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek67f28532009-06-17 22:02:04 +0000364 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000365 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000366};
367
368} // end anonymous namespace
369
Ted Kremenek9af46f52009-06-16 22:36:44 +0000370//===----------------------------------------------------------------------===//
371// RegionStore creation.
372//===----------------------------------------------------------------------===//
373
374StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
375 RegionStoreFeatures F = maximal_features_tag();
376 return new RegionStoreManager(StMgr, F);
377}
378
379StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
380 RegionStoreFeatures F = minimal_features_tag();
381 F.enableFields(true);
382 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000383}
384
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000385void
386RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000387 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000388 const MemRegion *superR = R->getSuperRegion();
389 if (add(superR, R))
390 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000391 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000392}
393
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000394RegionStoreSubRegionMap*
395RegionStoreManager::getRegionStoreSubRegionMap(const GRState *state) {
Ted Kremenek451ac092009-08-06 04:50:20 +0000396 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek59e8f112009-03-03 01:35:36 +0000397 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000399 llvm::SmallVector<const SubRegion*, 10> WL;
400
Ted Kremenek451ac092009-08-06 04:50:20 +0000401 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000402 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
403 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Ted Kremenek451ac092009-08-06 04:50:20 +0000405 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
406 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Mike Stump1eb44332009-09-09 15:08:12 +0000407 I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000408 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
409 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000410
Mike Stump1eb44332009-09-09 15:08:12 +0000411 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000412 // don't have direct bindings but are super regions of those that do.
413 while (!WL.empty()) {
414 const SubRegion *R = WL.back();
415 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000416 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000417 }
418
Ted Kremenek14453bf2009-03-03 19:02:42 +0000419 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000420}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000421
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000422SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
423 return getRegionStoreSubRegionMap(state);
424}
425
Ted Kremenek9af46f52009-06-16 22:36:44 +0000426//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000427// Binding invalidation.
428//===----------------------------------------------------------------------===//
429
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430void
Ted Kremenek451ac092009-08-06 04:50:20 +0000431RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
432 RegionDefaultBindings &DVM,
433 RegionDefaultBindings::Factory &DVMFactory,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000434 const MemRegion *R,
435 RegionStoreSubRegionMap &M) {
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437 RegionStoreSubRegionMap::iterator I, E;
438
439 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000440 RemoveSubRegionBindings(B, DVM, DVMFactory, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000442 B = RBFactory.Remove(B, R);
443 DVM = DVMFactory.Remove(DVM, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444}
445
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000446const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
447 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000448 const Expr *Ex,
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000449 unsigned Count) {
450 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000451
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000452 // Strip away casts.
453 R = R->getBaseRegion();
454
Ted Kremenek87806792009-09-27 20:45:21 +0000455 // Get the mapping of regions -> subregions.
456 llvm::OwningPtr<RegionStoreSubRegionMap>
457 SubRegions(getRegionStoreSubRegionMap(state));
458
459 RegionBindings B = GetRegionBindings(state->getStore());
460 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
461 RegionDefaultBindings::Factory &DVMFactory =
462 state->get_context<RegionDefaultValue>();
463
464 llvm::DenseMap<const MemRegion *, unsigned> Visited;
465 llvm::SmallVector<const MemRegion *, 10> WorkList;
466 WorkList.push_back(R);
467
468 while (!WorkList.empty()) {
469 R = WorkList.back();
470 WorkList.pop_back();
471
472 // Have we visited this region before?
473 unsigned &visited = Visited[R];
474 if (visited)
475 continue;
476 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Ted Kremenek87806792009-09-27 20:45:21 +0000478 // Add subregions to work list.
479 RegionStoreSubRegionMap::iterator I, E;
480 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
481 WorkList.push_back(*I);
482
483 // Handle region.
484 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
485 isa<ObjCObjectRegion>(R)) {
486 // Invalidate the region by setting its default value to
487 // conjured symbol. The type of the symbol is irrelavant.
488 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
489 Count);
490 DVM = DVMFactory.Add(DVM, R, V);
491 continue;
492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek87806792009-09-27 20:45:21 +0000494 if (!R->isBoundable())
495 continue;
496
497 const TypedRegion *TR = cast<TypedRegion>(R);
498 QualType T = TR->getValueType(Ctx);
499
500 if (const RecordType *RT = T->getAsStructureType()) {
501 // FIXME: handle structs with default region value.
502 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
503
504 // No record definition. There is nothing we can do.
505 if (!RD)
506 continue;
507
508 // Invalidate the region by setting its default value to
509 // conjured symbol. The type of the symbol is irrelavant.
510 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
511 Count);
512 DVM = DVMFactory.Add(DVM, R, V);
513 continue;
514 }
515
516 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
517 // Set the default value of the array to conjured symbol.
518 DefinedOrUnknownSVal V =
519 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
520 DVM = DVMFactory.Add(DVM, R, V);
521 continue;
522 }
523
524 // Get the old binding. Is it a region? If so, add it to the worklist.
525 if (const SVal *OldV = B.lookup(R)) {
526 if (const MemRegion *RV = OldV->getAsRegion())
527 WorkList.push_back(RV);
528 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000529
530 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
531 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
532 // For fields and elements whose super region has also been invalidated,
533 // only remove the old binding. The super region will get set with a
534 // default value from which we can lazily derive a new symbolic value.
535 B = RBFactory.Remove(B, R);
536 continue;
537 }
Ted Kremenek87806792009-09-27 20:45:21 +0000538
Ted Kremenek389c44c2009-09-29 03:12:50 +0000539 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000540 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
541 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
542 B = RBFactory.Add(B, R, V);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000543 }
544
Ted Kremenek87806792009-09-27 20:45:21 +0000545 // Create a new state with the updated bindings.
546 return state->makeWithStore(B.getRoot())->set<RegionDefaultValue>(DVM);
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000547}
548
549//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000550// getLValueXXX methods.
551//===----------------------------------------------------------------------===//
552
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000553/// getLValueString - Returns an SVal representing the lvalue of a
554/// StringLiteral. Within RegionStore a StringLiteral has an
555/// associated StringRegion, and the lvalue of a StringLiteral is the
556/// lvalue of that region.
Mike Stump1eb44332009-09-09 15:08:12 +0000557SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000558 const StringLiteral* S) {
559 return loc::MemRegionVal(MRMgr.getStringRegion(S));
560}
561
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000562/// getLValueVar - Returns an SVal that represents the lvalue of a
563/// variable. Within RegionStore a variable has an associated
564/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000565SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
566 const LocationContext *LC) {
567 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000568}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000569
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000570/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
571/// of a compound literal. Within RegionStore a compound literal
572/// has an associated region, and the lvalue of the compound literal
573/// is the lvalue of that region.
574SVal
Ted Kremenek67f28532009-06-17 22:02:04 +0000575RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
Mike Stump1eb44332009-09-09 15:08:12 +0000576 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000577 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
578}
579
Ted Kremenek67f28532009-06-17 22:02:04 +0000580SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000581 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000582 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000583}
584
Ted Kremenek67f28532009-06-17 22:02:04 +0000585SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000586 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000587 return getLValueFieldOrIvar(St, Base, D);
588}
589
Ted Kremenek67f28532009-06-17 22:02:04 +0000590SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000591 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592 if (Base.isUnknownOrUndef())
593 return Base;
594
595 Loc BaseL = cast<Loc>(Base);
596 const MemRegion* BaseR = 0;
597
598 switch (BaseL.getSubKind()) {
599 case loc::MemRegionKind:
600 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
601 break;
602
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000603 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000604 // These are anormal cases. Flag an undefined value.
605 return UndefinedVal();
606
607 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000608 // While these seem funny, this can happen through casts.
609 // FIXME: What we should return is the field offset. For example,
610 // add the field offset to the integer value. That way funny things
611 // like this work properly: &(((struct foo *) 0xa)->f)
612 return Base;
613
614 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000615 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000616 return Base;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000619 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
620 // of FieldDecl.
621 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
622 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000623
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000624 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000625}
626
Ted Kremenek67f28532009-06-17 22:02:04 +0000627SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000628 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000629 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000630
Ted Kremenekde7ec632009-03-09 22:44:49 +0000631 // If the base is an unknown or undefined value, just return it back.
632 // FIXME: For absolute pointer addresses, we just return that value back as
633 // well, although in reality we should return the offset added to that
634 // value.
635 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000636 return Base;
637
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000638 // Only handle integer offsets... for now.
639 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000640 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000641
Zhongxing Xuce760782009-05-09 13:20:07 +0000642 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000643
644 // Pointer of any type can be cast and used as array base.
645 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000646
Ted Kremenek46537392009-07-16 01:33:37 +0000647 // Convert the offset to the appropriate size and signedness.
648 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000650 if (!ElemR) {
651 //
652 // If the base region is not an ElementRegion, create one.
653 // This can happen in the following example:
654 //
655 // char *p = __builtin_alloc(10);
656 // p[1] = 8;
657 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000658 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000659 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000660 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000661 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000662 }
Mike Stump1eb44332009-09-09 15:08:12 +0000663
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000664 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000666 if (!isa<nonloc::ConcreteInt>(BaseIdx))
667 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000669 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
670 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
671 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Ted Kremenek46537392009-07-16 01:33:37 +0000673 // Compute the new index.
674 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Ted Kremenek46537392009-07-16 01:33:37 +0000676 // Construct the new ElementRegion.
677 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000678 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000679 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000680}
681
Ted Kremenek9af46f52009-06-16 22:36:44 +0000682//===----------------------------------------------------------------------===//
683// Extents for regions.
684//===----------------------------------------------------------------------===//
685
Ted Kremenek67f28532009-06-17 22:02:04 +0000686SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000687 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000689 switch (R->getKind()) {
690 case MemRegion::MemSpaceRegionKind:
691 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000692 return UnknownVal();
693
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000694 case MemRegion::CodeTextRegionKind:
695 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000696 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000697
698 // Not yet handled.
699 case MemRegion::AllocaRegionKind:
700 case MemRegion::CompoundLiteralRegionKind:
701 case MemRegion::ElementRegionKind:
702 case MemRegion::FieldRegionKind:
703 case MemRegion::ObjCIvarRegionKind:
704 case MemRegion::ObjCObjectRegionKind:
705 case MemRegion::SymbolicRegionKind:
706 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000708 case MemRegion::StringRegionKind: {
709 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000710 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000711 // operations with signed indices.
712 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000715 case MemRegion::VarRegionKind: {
716 const VarRegion* VR = cast<VarRegion>(R);
717 // Get the type of the variable.
718 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000720 // FIXME: Handle variable-length arrays.
721 if (isa<VariableArrayType>(T))
722 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000724 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
725 // return the size as signed integer.
726 return ValMgr.makeIntVal(CAT->getSize(), false);
727 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000728
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000729 // Clients can use ordinary variables as if they were arrays. These
730 // essentially are arrays of size 1.
731 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000734 case MemRegion::BEG_DECL_REGIONS:
735 case MemRegion::END_DECL_REGIONS:
736 case MemRegion::BEG_TYPED_REGIONS:
737 case MemRegion::END_TYPED_REGIONS:
738 assert(0 && "Infeasible region");
739 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000742 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000743 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000744}
745
Ted Kremenek67f28532009-06-17 22:02:04 +0000746const GRState *RegionStoreManager::setExtent(const GRState *state,
747 const MemRegion *region,
748 SVal extent) {
749 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000750}
751
752//===----------------------------------------------------------------------===//
753// Location and region casting.
754//===----------------------------------------------------------------------===//
755
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000756/// ArrayToPointer - Emulates the "decay" of an array to a pointer
757/// type. 'Array' represents the lvalue of the array being decayed
758/// to a pointer, and the returned SVal represents the decayed
759/// version of that lvalue (i.e., a pointer to the first element of
760/// the array). This is called by GRExprEngine when evaluating casts
761/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000762SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000763 if (!isa<loc::MemRegionVal>(Array))
764 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenekabb042f2008-12-13 19:24:37 +0000766 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
767 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000769 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000770 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000772 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000773 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000774 ArrayType *AT = cast<ArrayType>(T);
775 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek75185b52009-07-16 00:00:11 +0000777 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
778 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000779
780 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000781}
782
Ted Kremenek9af46f52009-06-16 22:36:44 +0000783//===----------------------------------------------------------------------===//
784// Pointer arithmetic.
785//===----------------------------------------------------------------------===//
786
Mike Stump1eb44332009-09-09 15:08:12 +0000787SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000788 BinaryOperator::Opcode Op, Loc L, NonLoc R,
789 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000790 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000791 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000792 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000793
Zhongxing Xua1718c72009-04-03 07:33:13 +0000794 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000795 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000796
Ted Kremenek3bccf082009-07-11 00:58:27 +0000797 switch (MR->getKind()) {
798 case MemRegion::SymbolicRegionKind: {
799 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000800 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000801 QualType T = Sym->getType(getContext());
802 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000804 if (const PointerType *PT = T->getAs<PointerType>())
805 EleTy = PT->getPointeeType();
806 else
John McCall183700f2009-09-21 23:43:11 +0000807 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Ted Kremenek3bccf082009-07-11 00:58:27 +0000809 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
810 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000811 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000812 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000813 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000814 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000815 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000816 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000817 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
818 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000819 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000820 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000821
Ted Kremenek3bccf082009-07-11 00:58:27 +0000822 case MemRegion::ElementRegionKind: {
823 ER = cast<ElementRegion>(MR);
824 break;
825 }
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenek3bccf082009-07-11 00:58:27 +0000827 // Not yet handled.
828 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000829 case MemRegion::StringRegionKind: {
830
831 }
832 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000833 case MemRegion::CompoundLiteralRegionKind:
834 case MemRegion::FieldRegionKind:
835 case MemRegion::ObjCObjectRegionKind:
836 case MemRegion::ObjCIvarRegionKind:
837 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek3bccf082009-07-11 00:58:27 +0000839 case MemRegion::CodeTextRegionKind:
840 // Technically this can happen if people do funny things with casts.
841 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek3bccf082009-07-11 00:58:27 +0000843 case MemRegion::MemSpaceRegionKind:
844 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
845 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Ted Kremenek3bccf082009-07-11 00:58:27 +0000847 case MemRegion::BEG_DECL_REGIONS:
848 case MemRegion::END_DECL_REGIONS:
849 case MemRegion::BEG_TYPED_REGIONS:
850 case MemRegion::END_TYPED_REGIONS:
851 assert(0 && "Infeasible region");
852 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000853 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000854
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000855 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000856 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000857
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000858 // For now, only support:
859 // (a) concrete integer indices that can easily be resolved
860 // (b) 0 + symbolic index
861 if (Base) {
862 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
863 // FIXME: Should use SValuator here.
864 SVal NewIdx =
865 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000866 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000867 const MemRegion* NewER =
868 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
869 ER->getSuperRegion(), getContext());
870 return ValMgr.makeLoc(NewER);
871 }
872 if (0 == Base->getValue()) {
873 const MemRegion* NewER =
874 MRMgr.getElementRegion(ER->getElementType(), R,
875 ER->getSuperRegion(), getContext());
876 return ValMgr.makeLoc(NewER);
877 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek5dc27462009-03-03 02:51:43 +0000880 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000881}
882
Ted Kremenek9af46f52009-06-16 22:36:44 +0000883//===----------------------------------------------------------------------===//
884// Loading values from regions.
885//===----------------------------------------------------------------------===//
886
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000887Optional<SVal> RegionStoreManager::getDefaultBinding(const GRState *state,
888 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000890 if (R->isBoundable())
891 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
892 if (TR->getValueType(getContext())->isUnionType())
893 return UnknownVal();
894
895 return Optional<SVal>::create(state->get<RegionDefaultValue>(R));
896}
897
Ted Kremeneka6275a52009-07-15 02:31:43 +0000898static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
899 RTy = Ctx.getCanonicalType(RTy);
900 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Ted Kremeneka6275a52009-07-15 02:31:43 +0000902 if (RTy == UsedTy)
903 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
905
Ted Kremenek25c54572009-07-20 22:58:02 +0000906 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000907 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000908 // represents a reinterpretation.
909 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000910 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000911 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000912
913 return PUsedTy && PRTy &&
914 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000915 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000916 }
917
918 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000919}
920
Ted Kremenek0954cde2009-09-24 04:11:44 +0000921const ElementRegion *
922RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
923 ASTContext &Ctx = getContext();
924 SVal idx = ValMgr.makeZeroArrayIndex();
925 assert(!T.isNull());
926 return MRMgr.getElementRegion(T, idx, SR, Ctx);
927}
928
929
930
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000931SValuator::CastResult
932RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000933
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000934 assert(!isa<UnknownVal>(L) && "location unknown");
935 assert(!isa<UndefinedVal>(L) && "location undefined");
936
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000937 // FIXME: Is this even possible? Shouldn't this be treated as a null
938 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000939 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000940 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000941
Ted Kremenek67f28532009-06-17 22:02:04 +0000942 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000943
Zhongxing Xu91844122009-05-20 09:18:48 +0000944 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000945 // Example:
946 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000947 // char* p = alloca();
948 // read(p);
949 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000950 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000951 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Ted Kremenek0954cde2009-09-24 04:11:44 +0000953 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
954 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Ted Kremenek968f0a62009-08-03 21:41:46 +0000956 if (isa<CodeTextRegion>(MR))
957 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000959 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
960 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000961 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000962 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000963
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000964 // FIXME: We should eventually handle funny addressing. e.g.:
965 //
966 // int x = ...;
967 // int *p = &x;
968 // char *q = (char*) p;
969 // char c = *q; // returns the first byte of 'x'.
970 //
971 // Such funny addressing will occur due to layering of regions.
972
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000973#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000974 ASTContext &Ctx = getContext();
975 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +0000976 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
977 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000978 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +0000979 assert(Ctx.getCanonicalType(RTy) ==
980 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +0000981 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000982#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000983
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000984 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000985 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000987 // FIXME: Handle unions.
988 if (RTy->isUnionType())
989 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000990
991 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000992 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +0000993
Zhongxing Xu1038f9f2009-03-09 09:15:51 +0000994 // FIXME: handle Vector types.
995 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000996 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +0000997
998 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000999 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001000
1001 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001002 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Ted Kremenek25c54572009-07-20 22:58:02 +00001004 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001005 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Ted Kremenek9031dd72009-07-21 00:12:07 +00001007 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001008 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001009
Ted Kremenek451ac092009-08-06 04:50:20 +00001010 RegionBindings B = GetRegionBindings(state->getStore());
1011 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001012
1013 // Check if the region has a binding.
1014 if (V)
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001015 return SValuator::CastResult(state, *V);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001016
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001017 // The location does not have a bound value. This means that it has
1018 // the value it had upon its creation and/or entry to the analyzed
1019 // function/method. These are either symbolic values or 'undefined'.
1020
Ted Kremenek356e9d62009-07-22 04:35:42 +00001021#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001022 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001023#else
1024 if (R->hasStackStorage()) {
1025#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001026 // All stack variables are considered to have undefined values
1027 // upon creation. All heap allocated blocks are considered to
1028 // have undefined values as well unless they are explicitly bound
1029 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001030 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001031 }
1032
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001033 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001034 return SValuator::CastResult(state,
1035 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001036}
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001038std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001039RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001040
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001041 if (const nonloc::LazyCompoundVal *V =
1042 dyn_cast_or_null<nonloc::LazyCompoundVal>(B.lookup(R)))
1043 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001045 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1046 const std::pair<const GRState *, const MemRegion *> &X =
1047 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001049 if (X.first)
1050 return std::make_pair(X.first,
1051 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001052 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001053 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1054 const std::pair<const GRState *, const MemRegion *> &X =
1055 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001057 if (X.first)
1058 return std::make_pair(X.first,
1059 MRMgr.getFieldRegionWithSuper(FR, X.second));
1060 }
1061
1062 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1063}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001064
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001065SVal RegionStoreManager::RetrieveElement(const GRState* state,
1066 const ElementRegion* R) {
1067 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001068 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek921109a2009-07-01 23:19:52 +00001069 if (const SVal* V = B.lookup(R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001070 return *V;
1071
Ted Kremenek921109a2009-07-01 23:19:52 +00001072 const MemRegion* superR = R->getSuperRegion();
1073
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001074 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001075 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001076 // FIXME: Handle loads from strings where the literal is treated as
1077 // an integer, e.g., *((unsigned int*)"hello")
1078 ASTContext &Ctx = getContext();
1079 QualType T = StrR->getValueType(Ctx)->getAs<ArrayType>()->getElementType();
1080 if (T != Ctx.getCanonicalType(R->getElementType()))
1081 return UnknownVal();
1082
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001083 const StringLiteral *Str = StrR->getStringLiteral();
1084 SVal Idx = R->getIndex();
1085 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1086 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001087 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001088 if (i > byteLength) {
1089 // Buffer overflow checking in GRExprEngine should handle this case,
1090 // but we shouldn't rely on it to not overflow here if that checking
1091 // is disabled.
1092 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001093 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001094 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001095 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001096 }
1097 }
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001099 // Special case: the current region represents a cast and it and the super
1100 // region both have pointer types or intptr_t types. If so, perform the
1101 // retrieve from the super region and appropriately "cast" the value.
1102 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001103 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001104 if (R->getIndex().isZeroConstant()) {
1105 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1106 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001107 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1108 QualType valTy = R->getValueType(Ctx);
1109 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1110 // Retrieve the value from the super region. This will be casted to
1111 // valTy when we return to 'Retrieve'.
1112 const SValuator::CastResult &cr = Retrieve(state,
1113 loc::MemRegionVal(superR),
1114 valTy);
1115 return cr.getSVal();
1116 }
1117 }
1118 }
1119 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001120
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001121 // Check if the immediate super region has a direct binding.
Ted Kremeneka6275a52009-07-15 02:31:43 +00001122 if (const SVal *V = B.lookup(superR)) {
1123 if (SymbolRef parentSym = V->getAsSymbol())
1124 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001125
1126 if (V->isUnknownOrUndef())
1127 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001128
1129 // Handle LazyCompoundVals for the immediate super region. Other cases
1130 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001131 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001132 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001134 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1135 return RetrieveElement(LCV->getState(), R);
1136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Ted Kremeneka6275a52009-07-15 02:31:43 +00001138 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001139 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001140 }
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001142 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001143}
1144
Mike Stump1eb44332009-09-09 15:08:12 +00001145SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001146 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001147
1148 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001149 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek8b2ba312009-07-01 23:30:34 +00001150 if (const SVal* V = B.lookup(R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001151 return *V;
1152
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001153 QualType Ty = R->getValueType(getContext());
1154 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1155}
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001157SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1158 const TypedRegion *R,
1159 QualType Ty,
1160 const MemRegion *superR) {
1161
Mike Stump1eb44332009-09-09 15:08:12 +00001162 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001163 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001165 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001167 while (superR) {
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001168 if (const Optional<SVal> &D = getDefaultBinding(state, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001169 if (SymbolRef parentSym = D->getAsSymbol())
1170 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001172 if (D->isZeroConstant())
1173 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001175 if (D->isUnknown())
1176 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001177
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001178 assert(0 && "Unknown default value");
1179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001181 // If our super region is a field or element itself, walk up the region
1182 // hierarchy to see if there is a default value installed in an ancestor.
1183 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1184 superR = cast<SubRegion>(superR)->getSuperRegion();
1185 continue;
1186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001188 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001191 // Lazy binding?
1192 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001193 const MemRegion *lazyBindingRegion = NULL;
1194 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001196 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001197 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001199 if (isa<ElementRegion>(R))
1200 return RetrieveElement(lazyBindingState,
1201 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001203 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001204 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001205 }
1206
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001207 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001208
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001209 if (isa<ElementRegion>(R)) {
1210 // Currently we don't reason specially about Clang-style vectors. Check
1211 // if superR is a vector and if so return Unknown.
1212 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1213 if (typedSuperR->getValueType(getContext())->isVectorType())
1214 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001215 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001218 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001221 // All other values are symbolic.
1222 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001223}
Mike Stump1eb44332009-09-09 15:08:12 +00001224
1225SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001226 const ObjCIvarRegion* R) {
1227
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001228 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001229 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001230
1231 if (const SVal* V = B.lookup(R))
1232 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001234 const MemRegion *superR = R->getSuperRegion();
1235
1236 // Check if the super region has a binding.
1237 if (const SVal *V = B.lookup(superR)) {
1238 if (SymbolRef parentSym = V->getAsSymbol())
1239 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001241 // Other cases: give up.
1242 return UnknownVal();
1243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Ted Kremenek25c54572009-07-20 22:58:02 +00001245 return RetrieveLazySymbol(state, R);
1246}
1247
Ted Kremenek9031dd72009-07-21 00:12:07 +00001248SVal RegionStoreManager::RetrieveVar(const GRState *state,
1249 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremenek9031dd72009-07-21 00:12:07 +00001251 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001252 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Ted Kremenek9031dd72009-07-21 00:12:07 +00001254 if (const SVal* V = B.lookup(R))
1255 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek9031dd72009-07-21 00:12:07 +00001257 // Lazily derive a value for the VarRegion.
1258 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenek9031dd72009-07-21 00:12:07 +00001260 if (R->hasGlobalsOrParametersStorage())
1261 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Ted Kremenek9031dd72009-07-21 00:12:07 +00001263 return UndefinedVal();
1264}
1265
Mike Stump1eb44332009-09-09 15:08:12 +00001266SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001267 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek25c54572009-07-20 22:58:02 +00001269 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001270
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001271 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001272 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001273}
1274
Mike Stump1eb44332009-09-09 15:08:12 +00001275SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1276 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001277 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001278 assert(T->isStructureType());
1279
Zhongxing Xub7507d12009-06-11 07:27:30 +00001280 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001281 RecordDecl* RD = RT->getDecl();
1282 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001283 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001284#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001285 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1286
Ted Kremenek67f28532009-06-17 22:02:04 +00001287 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1288 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001289 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001290
Douglas Gregore267ff32008-12-11 20:41:00 +00001291 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1292 FieldEnd = Fields.rend();
1293 Field != FieldEnd; ++Field) {
1294 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001295 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001296 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001297 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1298 }
1299
Zhongxing Xud91ee272009-06-23 09:02:15 +00001300 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001301#else
1302 return ValMgr.makeLazyCompoundVal(state, R);
1303#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001304}
1305
Ted Kremenek67f28532009-06-17 22:02:04 +00001306SVal RegionStoreManager::RetrieveArray(const GRState *state,
1307 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001308#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001309 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001310 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1311
1312 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001313 uint64_t size = CAT->getSize().getZExtValue();
1314 for (uint64_t i = 0; i < size; ++i) {
1315 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001316 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001317 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001318 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001319 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001320 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1321 }
1322
Zhongxing Xud91ee272009-06-23 09:02:15 +00001323 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001324#else
1325 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1326 return ValMgr.makeLazyCompoundVal(state, R);
1327#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001328}
1329
Ted Kremenek9af46f52009-06-16 22:36:44 +00001330//===----------------------------------------------------------------------===//
1331// Binding values to regions.
1332//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001333
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001334Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001335 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Ted Kremenek0964a062009-01-21 06:57:53 +00001337 if (isa<loc::MemRegionVal>(L))
1338 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Ted Kremenek0964a062009-01-21 06:57:53 +00001340 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001341 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001342 return RBFactory.Remove(B, R).getRoot();
1343 }
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Ted Kremenek0964a062009-01-21 06:57:53 +00001345 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001346}
1347
Ted Kremenek67f28532009-06-17 22:02:04 +00001348const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001349 if (isa<loc::ConcreteInt>(L))
1350 return state;
1351
Ted Kremenek9af46f52009-06-16 22:36:44 +00001352 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001353 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Ted Kremenek9af46f52009-06-16 22:36:44 +00001355 // Check if the region is a struct region.
1356 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1357 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001358 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001360 // Special case: the current region represents a cast and it and the super
1361 // region both have pointer types or intptr_t types. If so, perform the
1362 // bind to the super region.
1363 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001364 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001365 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1366 if (ER->getIndex().isZeroConstant()) {
1367 if (const TypedRegion *superR =
1368 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1369 ASTContext &Ctx = getContext();
1370 QualType superTy = superR->getValueType(Ctx);
1371 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
1373 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001374 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001375 SValuator::CastResult cr =
1376 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001377 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1378 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001379 // For now, just invalidate the fields of the struct/union/class.
1380 // FIXME: Precisely handle the fields of the record.
1381 if (superTy->isRecordType())
1382 return InvalidateRegion(state, superR, NULL, 0);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001383 }
1384 }
1385 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001386 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1387 // Binding directly to a symbolic region should be treated as binding
1388 // to element 0.
1389 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001390 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001391 R = GetElementZeroRegion(SR, T);
1392 }
Mike Stump1eb44332009-09-09 15:08:12 +00001393
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001394 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001395 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001396 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001397}
1398
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001399const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1400 const VarDecl *VD,
1401 const LocationContext *LC,
1402 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001403
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001404 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001405 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001406
Ted Kremenek0964a062009-01-21 06:57:53 +00001407 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001408 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001409 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001410 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001411
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001412 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001413}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001414
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001415// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001416const GRState *
1417RegionStoreManager::BindCompoundLiteral(const GRState *state,
1418 const CompoundLiteralExpr* CL,
1419 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001421 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001422 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001423}
1424
Ted Kremenek67f28532009-06-17 22:02:04 +00001425const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001426 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001427 SVal Init) {
1428
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001429 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001430 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001431 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001432
Ted Kremenek46537392009-07-16 01:33:37 +00001433 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001434
1435 // Check if the init expr is a StringLiteral.
1436 if (isa<loc::MemRegionVal>(Init)) {
1437 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1438 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1439 const char* str = S->getStrData();
1440 unsigned len = S->getByteLength();
1441 unsigned j = 0;
1442
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001443 // Copy bytes from the string literal into the target array. Trailing bytes
1444 // in the array that are not covered by the string literal are initialized
1445 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001446 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001447 if (j >= len)
1448 break;
1449
Ted Kremenek46537392009-07-16 01:33:37 +00001450 SVal Idx = ValMgr.makeArrayIndex(i);
1451 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1452 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001453
Zhongxing Xud91ee272009-06-23 09:02:15 +00001454 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001455 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001456 }
1457
Ted Kremenek67f28532009-06-17 22:02:04 +00001458 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001459 }
1460
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001461 // Handle lazy compound values.
1462 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1463 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001464
1465 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001466 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001467 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001468 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Ted Kremenek46537392009-07-16 01:33:37 +00001470 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001471 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001472 if (VI == VE)
1473 break;
1474
Ted Kremenek46537392009-07-16 01:33:37 +00001475 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001476 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001477
1478 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001479 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001480 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001481 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001482 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001483 }
1484
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001485 // If the init list is shorter than the array length, set the array default
1486 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001487 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001488 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001489 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001490 state = setDefaultValue(state, R, V);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001491 }
1492 }
1493
Ted Kremenek67f28532009-06-17 22:02:04 +00001494 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001495}
1496
Ted Kremenek67f28532009-06-17 22:02:04 +00001497const GRState *
1498RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1499 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Ted Kremenek67f28532009-06-17 22:02:04 +00001501 if (!Features.supportsFields())
1502 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001504 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001505 assert(T->isStructureType());
1506
Ted Kremenek6217b802009-07-29 21:53:49 +00001507 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001508 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001509
1510 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001511 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001512
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001513 // Handle lazy compound values.
1514 if (const nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&V))
1515 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Ted Kremenek67f28532009-06-17 22:02:04 +00001517 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1518 // Ignore them and kill the field values.
1519 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
1520 return KillStruct(state, R);
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001521
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001522 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001523 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001524
1525 RecordDecl::field_iterator FI, FE;
1526
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001527 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001528
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001529 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001530 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001531
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001532 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001533 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001534
Ted Kremenekcf549592009-09-22 21:19:14 +00001535 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001536 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001537 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001538 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001539 else
1540 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001541 }
1542
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001543 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001544 if (FI != FE)
1545 state = setDefaultValue(state, R, ValMgr.makeIntVal(0, false));
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001546
Ted Kremenek67f28532009-06-17 22:02:04 +00001547 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001548}
1549
Ted Kremenek67f28532009-06-17 22:02:04 +00001550const GRState *RegionStoreManager::KillStruct(const GRState *state,
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001551 const TypedRegion* R){
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001552
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001553 // Set the default value of the struct region to "unknown".
1554 state = state->set<RegionDefaultValue>(R, UnknownVal());
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001555
1556 // Remove all bindings for the subregions of the struct.
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001557 Store store = state->getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001558 RegionBindings B = GetRegionBindings(store);
1559 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek67f28532009-06-17 22:02:04 +00001560 const MemRegion* R = I.getKey();
1561 if (const SubRegion* subRegion = dyn_cast<SubRegion>(R))
1562 if (subRegion->isSubRegionOf(R))
Zhongxing Xud91ee272009-06-23 09:02:15 +00001563 store = Remove(store, ValMgr.makeLoc(subRegion));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001564 }
1565
Ted Kremenek67f28532009-06-17 22:02:04 +00001566 return state->makeWithStore(store);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001567}
1568
Ted Kremenek67f28532009-06-17 22:02:04 +00001569const GRState *RegionStoreManager::setDefaultValue(const GRState *state,
1570 const MemRegion* R, SVal V) {
1571 return state->set<RegionDefaultValue>(R, V);
Zhongxing Xu264e9372009-05-12 10:10:00 +00001572}
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001574const GRState*
1575RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1576 const GRState *state,
1577 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001578
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001579 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001580 RegionBindings B = GetRegionBindings(state->getStore());
1581 RegionDefaultBindings DVM = state->get<RegionDefaultValue>();
1582 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001583 state->get_context<RegionDefaultValue>();
1584
Mike Stump1eb44332009-09-09 15:08:12 +00001585 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001586 SubRegions(getRegionStoreSubRegionMap(state));
1587
Mike Stump1eb44332009-09-09 15:08:12 +00001588 // B and DVM are updated after the call to RemoveSubRegionBindings.
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001589 RemoveSubRegionBindings(B, DVM, DVMFactory, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001591 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1592 // results in a zero-copy algorithm.
1593 return state->makeWithStore(RBFactory.Add(B, R, V).getRoot());
1594}
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Ted Kremenek9af46f52009-06-16 22:36:44 +00001596//===----------------------------------------------------------------------===//
1597// State pruning.
1598//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Ted Kremenek451ac092009-08-06 04:50:20 +00001600namespace {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001601class VISIBILITY_HIDDEN RBDNode
1602 : public std::pair<const GRState*, const MemRegion *> {
Ted Kremenek451ac092009-08-06 04:50:20 +00001603public:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001604 RBDNode(const GRState *st, const MemRegion *r)
1605 : std::pair<const GRState*, const MemRegion*>(st, r) {}
1606
1607 const GRState *getState() const { return first; }
1608 const MemRegion *getRegion() const { return second; }
1609};
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001611enum VisitFlag { NotVisited = 0, VisitedFromSubRegion, VisitedFromSuperRegion };
1612
1613class RBDItem : public RBDNode {
1614private:
1615 const VisitFlag VF;
1616
1617public:
1618 RBDItem(const GRState *st, const MemRegion *r, VisitFlag vf)
1619 : RBDNode(st, r), VF(vf) {}
1620
1621 VisitFlag getVisitFlag() const { return VF; }
Ted Kremenek451ac092009-08-06 04:50:20 +00001622};
1623} // end anonymous namespace
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001624
Mike Stump1eb44332009-09-09 15:08:12 +00001625void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001626 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001627 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001628{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001629 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001630 RegionBindings B = GetRegionBindings(store);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001631 RegionDefaultBindings DVM = state.get<RegionDefaultValue>();
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenek9af46f52009-06-16 22:36:44 +00001633 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001634 llvm::OwningPtr<RegionStoreSubRegionMap>
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001635 SubRegions(getRegionStoreSubRegionMap(&state));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001636
1637 // Do a pass over the regions in the store. For VarRegions we check if
1638 // the variable is still live and if so add it to the list of live roots.
1639 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001640 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001641
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001642 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001643 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001644 const MemRegion *R = I.getKey();
1645 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001646 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001647
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001648 // Scan the default bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001649 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001650 I != E; ++I) {
1651 const MemRegion *R = I.getKey();
1652 IntermediateRoots.push_back(R);
1653 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001654
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001655 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001656 // real roots.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001657 llvm::SmallVector<RBDItem, 10> WorkList;
1658 llvm::DenseMap<const MemRegion*,unsigned> IntermediateVisited;
1659
Ted Kremenek9af46f52009-06-16 22:36:44 +00001660 while (!IntermediateRoots.empty()) {
1661 const MemRegion* R = IntermediateRoots.back();
1662 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001663
1664 unsigned &visited = IntermediateVisited[R];
1665 if (visited)
1666 continue;
1667 visited = 1;
1668
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001670 if (SymReaper.isLive(Loc, VR->getDecl()))
1671 WorkList.push_back(RBDItem(&state, VR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001672 continue;
1673 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001674
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001675 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001676 if (SymReaper.isLive(SR->getSymbol()))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001677 WorkList.push_back(RBDItem(&state, SR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001678 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001679 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001680
1681 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001683 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001684 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001685 }
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001687 // Enqueue the RegionRoots onto WorkList.
1688 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1689 E=RegionRoots.end(); I!=E; ++I) {
1690 WorkList.push_back(RBDItem(&state, *I, VisitedFromSuperRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001691 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001692 RegionRoots.clear();
1693
1694 // Process the worklist.
1695 typedef llvm::DenseMap<std::pair<const GRState*, const MemRegion*>, VisitFlag>
1696 VisitMap;
1697
1698 VisitMap Visited;
1699
1700 while (!WorkList.empty()) {
1701 RBDItem N = WorkList.back();
1702 WorkList.pop_back();
1703
1704 // Have we visited this node before?
1705 VisitFlag &VF = Visited[N];
1706 if (VF >= N.getVisitFlag())
1707 continue;
1708
1709 const MemRegion *R = N.getRegion();
1710 const GRState *state_N = N.getState();
1711
1712 // Enqueue subregions?
1713 if (N.getVisitFlag() == VisitedFromSuperRegion) {
1714 RegionStoreSubRegionMap *M;
1715
1716 if (&state == state_N)
1717 M = SubRegions.get();
1718 else {
1719 RegionStoreSubRegionMap *& SM = SC[state_N];
1720 if (!SM)
1721 SM = getRegionStoreSubRegionMap(state_N);
1722 M = SM;
1723 }
1724
1725 RegionStoreSubRegionMap::iterator I, E;
1726 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1727 WorkList.push_back(RBDItem(state_N, *I, VisitedFromSuperRegion));
1728 }
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001730 // At this point, if we have already visited this region before, we are
1731 // done.
1732 if (VF != NotVisited) {
1733 VF = N.getVisitFlag();
1734 continue;
1735 }
1736 VF = N.getVisitFlag();
1737
1738 // Enqueue the super region.
1739 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1740 const MemRegion *superR = SR->getSuperRegion();
1741 if (!isa<MemSpaceRegion>(superR)) {
1742 // If 'R' is a field or an element, we want to keep the bindings
1743 // for the other fields and elements around. The reason is that
1744 // pointer arithmetic can get us to the other fields or elements.
1745 VisitFlag NewVisit =
1746 isa<FieldRegion>(R) || isa<ElementRegion>(R) || isa<ObjCIvarRegion>(R)
1747 ? VisitedFromSuperRegion : VisitedFromSubRegion;
1748
1749 WorkList.push_back(RBDItem(state_N, superR, NewVisit));
1750 }
1751 }
1752
1753 // Mark the symbol for any live SymbolicRegion as "live". This means we
1754 // should continue to track that symbol.
1755 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1756 SymReaper.markLive(SymR->getSymbol());
1757
1758 Store store_N = state_N->getStore();
1759 RegionBindings B_N = GetRegionBindings(store_N);
1760
1761 // Get the data binding for R (if any).
1762 const SVal* Xptr = B_N.lookup(R);
1763
1764 // Check for lazy bindings.
1765 if (const nonloc::LazyCompoundVal *V =
1766 dyn_cast_or_null<nonloc::LazyCompoundVal>(Xptr)) {
1767
1768 const LazyCompoundValData *D = V->getCVData();
1769 WorkList.push_back(RBDItem(D->getState(), D->getRegion(),
1770 VisitedFromSuperRegion));
1771 }
1772 else {
1773 // No direct binding? Get the default binding for R (if any).
1774 if (!Xptr) {
1775 RegionDefaultBindings DVM_N = &state == state_N ? DVM
1776 : state_N->get<RegionDefaultValue>();
1777
1778 Xptr = DVM_N.lookup(R);
1779 }
1780
1781 // Direct or default binding?
1782 if (Xptr) {
1783 SVal X = *Xptr;
1784
1785 // Update the set of live symbols.
1786 for (SVal::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end();
1787 SI!=SE;++SI)
1788 SymReaper.markLive(*SI);
1789
1790 // If X is a region, then add it to the worklist.
1791 if (const MemRegion *RX = X.getAsRegion())
1792 WorkList.push_back(RBDItem(state_N, RX, VisitedFromSuperRegion));
1793 }
1794 }
1795 }
1796
Ted Kremenek9af46f52009-06-16 22:36:44 +00001797 // We have now scanned the store, marking reachable regions and symbols
1798 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001799 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001800 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001801 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001802 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001803 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001807 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Ted Kremenek9af46f52009-06-16 22:36:44 +00001809 // Mark all non-live symbols that this region references as dead.
1810 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1811 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Ted Kremenek9af46f52009-06-16 22:36:44 +00001813 SVal X = I.getData();
1814 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001815 for (; SI != SE; ++SI)
1816 SymReaper.maybeDead(*SI);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
1819 // Remove dead 'default' bindings.
Ted Kremenek451ac092009-08-06 04:50:20 +00001820 RegionDefaultBindings NewDVM = DVM;
Mike Stump1eb44332009-09-09 15:08:12 +00001821 RegionDefaultBindings::Factory &DVMFactory =
Ted Kremenek093569c2009-08-02 05:00:15 +00001822 state.get_context<RegionDefaultValue>();
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek451ac092009-08-06 04:50:20 +00001824 for (RegionDefaultBindings::iterator I = DVM.begin(), E = DVM.end();
Ted Kremenek093569c2009-08-02 05:00:15 +00001825 I != E; ++I) {
1826 const MemRegion *R = I.getKey();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Ted Kremenek093569c2009-08-02 05:00:15 +00001828 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001829 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek093569c2009-08-02 05:00:15 +00001830 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Ted Kremenek093569c2009-08-02 05:00:15 +00001832 // Remove this dead region.
1833 NewDVM = DVMFactory.Remove(NewDVM, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Ted Kremenek093569c2009-08-02 05:00:15 +00001835 // Mark all non-live symbols that this region references as dead.
1836 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1837 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Ted Kremenek093569c2009-08-02 05:00:15 +00001839 SVal X = I.getData();
1840 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1841 for (; SI != SE; ++SI)
1842 SymReaper.maybeDead(*SI);
1843 }
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001845 // Write the store back.
1846 state.setStore(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Ted Kremenek093569c2009-08-02 05:00:15 +00001848 // Write the updated default bindings back.
1849 // FIXME: Right now this involves a fetching of a persistent state.
1850 // We can do better.
1851 if (DVM != NewDVM)
1852 state.setGDM(state.set<RegionDefaultValue>(NewDVM)->getGDM());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001853}
1854
1855//===----------------------------------------------------------------------===//
1856// Utility methods.
1857//===----------------------------------------------------------------------===//
1858
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001859void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001860 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001861 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001862 OS << "Store (direct bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Ted Kremenek451ac092009-08-06 04:50:20 +00001864 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001865 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001866}