blob: af2e359a00e4d92b546dc6b279b68211067343aa [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 Xu13d50172009-10-11 08:08:02 +000035namespace {
36class BindingVal {
37public:
38 enum BindingKind { Direct, Default };
39private:
40 SVal Value;
41 BindingKind Kind;
42
43public:
44 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
45
46 bool isDefault() const { return Kind == Default; }
47
48 const SVal *getValue() const { return &Value; }
49
50 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
51
52 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
53
54 void Profile(llvm::FoldingSetNodeID& ID) const {
55 Value.Profile(ID);
56 ID.AddInteger(Kind);
57 }
58
59 inline bool operator==(const BindingVal& R) const {
60 return Value == R.Value && Kind == R.Kind;
61 }
62
63 inline bool operator!=(const BindingVal& R) const {
64 return !(*this == R);
65 }
66};
67}
68
69namespace llvm {
70static inline
71llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
72 if (V.isDefault())
73 os << "(default) ";
74 else
75 os << "(direct) ";
76 os << *V.getValue();
77 return os;
78}
79} // end llvm namespace
80
Zhongxing Xubaf03a72008-11-24 09:44:56 +000081// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000082typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000083
Ted Kremenek50dc1b32008-12-24 01:05:03 +000084//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000085// Fine-grained control of RegionStoreManager.
86//===----------------------------------------------------------------------===//
87
88namespace {
89struct VISIBILITY_HIDDEN minimal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000090struct VISIBILITY_HIDDEN maximal_features_tag {};
91
Ted Kremenek9af46f52009-06-16 22:36:44 +000092class VISIBILITY_HIDDEN RegionStoreFeatures {
93 bool SupportsFields;
94 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenek9af46f52009-06-16 22:36:44 +000096public:
97 RegionStoreFeatures(minimal_features_tag) :
98 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek9af46f52009-06-16 22:36:44 +0000100 RegionStoreFeatures(maximal_features_tag) :
101 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek9af46f52009-06-16 22:36:44 +0000105 bool supportsFields() const { return SupportsFields; }
106 bool supportsRemaining() const { return SupportsRemaining; }
107};
108}
109
110//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111// Region "Extents"
112//===----------------------------------------------------------------------===//
113//
114// MemRegions represent chunks of memory with a size (their "extent"). This
115// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000116//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
118static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000119namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000120 template<> struct GRStateTrait<RegionExtents>
121 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
122 static void* GDMIndex() { return &RegionExtentsIndex; }
123 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000124}
125
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000126//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000127// Utility functions.
128//===----------------------------------------------------------------------===//
129
130static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
131 if (ty->isAnyPointerType())
132 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000134 return ty->isIntegerType() && ty->isScalarType() &&
135 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
136}
137
138//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000139// Main RegionStore logic.
140//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000141
Zhongxing Xu17892752008-10-08 02:50:44 +0000142namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
145 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000146 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000147 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000150 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000151 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000152
153 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000154 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000155 return true;
156 }
157
158 I->second = F.Add(I->second, SubRegion);
159 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000162 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek59e8f112009-03-03 01:35:36 +0000164 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek5dc27462009-03-03 02:51:43 +0000166 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000167 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000168
169 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000170 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 llvm::ImmutableSet<const MemRegion*> S = I->second;
173 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
174 SI != SE; ++SI) {
175 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000176 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek5dc27462009-03-03 02:51:43 +0000179 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000182 typedef SetTy::iterator iterator;
183
184 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
185 Map::iterator I = M.find(R);
186 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
187 return std::make_pair(S.begin(), S.end());
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000190
Zhongxing Xu17892752008-10-08 02:50:44 +0000191class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000192 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000193 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000194
195 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
196 SMCache SC;
197
Zhongxing Xu17892752008-10-08 02:50:44 +0000198public:
Mike Stump1eb44332009-09-09 15:08:12 +0000199 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000200 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000202 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000203
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000204 virtual ~RegionStoreManager() {
205 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
206 delete (*I).second;
207 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000208
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000209 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Zhongxing Xu13d50172009-10-11 08:08:02 +0000211 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Zhongxing Xu13d50172009-10-11 08:08:02 +0000213 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
214 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000215 /// getDefaultBinding - Returns an SVal* representing an optional default
216 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000217 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000218
219 /// setImplicitDefaultValue - Set the default binding for the provided
220 /// MemRegion to the value implicitly defined for compound literals when
221 /// the value is not specified.
222 const GRState *setImplicitDefaultValue(const GRState *state,
223 const MemRegion *R,
224 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000226 /// getLValueString - Returns an SVal representing the lvalue of a
227 /// StringLiteral. Within RegionStore a StringLiteral has an
228 /// associated StringRegion, and the lvalue of a StringLiteral is
229 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000230 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000231
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000232 /// getLValueCompoundLiteral - Returns an SVal representing the
233 /// lvalue of a compound literal. Within RegionStore a compound
234 /// literal has an associated region, and the lvalue of the
235 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000236 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000237
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000238 /// getLValueVar - Returns an SVal that represents the lvalue of a
239 /// variable. Within RegionStore a variable has an associated
240 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000241 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000243 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000244
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000245 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000247 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000248
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000249 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000250
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000251
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000252 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
253 /// type. 'Array' represents the lvalue of the array being decayed
254 /// to a pointer, and the returned SVal represents the decayed
255 /// version of that lvalue (i.e., a pointer to the first element of
256 /// the array). This is called by GRExprEngine when evaluating
257 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000258 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000259
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000260 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000261 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000262
Mike Stump1eb44332009-09-09 15:08:12 +0000263 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000264 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000265 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000266
Ted Kremenek67f28532009-06-17 22:02:04 +0000267 //===-------------------------------------------------------------------===//
268 // Binding values to regions.
269 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000270
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000271 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000272 const Expr *E, unsigned Count,
273 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000275private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000276 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000277 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
279public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000280 const GRState *Bind(const GRState *state, Loc LV, SVal V);
281
282 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000283 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000285 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
286 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000287
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000288 const GRState *BindDeclWithNoInit(const GRState *state,
289 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000290 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000291 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000292
Ted Kremenek67f28532009-06-17 22:02:04 +0000293 /// BindStruct - Bind a compound value to a structure.
294 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenek67f28532009-06-17 22:02:04 +0000296 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
298 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000299 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000300
Ted Kremenek67f28532009-06-17 22:02:04 +0000301 Store Remove(Store store, Loc LV);
302
303 //===------------------------------------------------------------------===//
304 // Loading values from regions.
305 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenek67f28532009-06-17 22:02:04 +0000307 /// The high level logic for this method is this:
308 /// Retrieve (L)
309 /// if L has binding
310 /// return L's binding
311 /// else if L is in killset
312 /// return unknown
313 /// else
314 /// if L is on stack or heap
315 /// return undefined
316 /// else
317 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000318 SValuator::CastResult Retrieve(const GRState *state, Loc L,
319 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000320
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000321 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000322
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000323 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000325 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremenek9031dd72009-07-21 00:12:07 +0000327 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek25c54572009-07-20 22:58:02 +0000329 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000331 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
332 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek67f28532009-06-17 22:02:04 +0000334 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000335 /// struct copy:
336 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000337 /// x = y;
338 /// y's value is retrieved by this method.
339 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000343 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000344 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000346 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
347 const GRState *state,
348 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000349
Ted Kremenek0954cde2009-09-24 04:11:44 +0000350 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
351 QualType T);
352
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 //===------------------------------------------------------------------===//
354 // State pruning.
355 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
358 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000359 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
361
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000362 const GRState *EnterStackFrame(const GRState *state,
363 const StackFrameContext *frame);
364
Ted Kremenek67f28532009-06-17 22:02:04 +0000365 //===------------------------------------------------------------------===//
366 // Region "extents".
367 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek67f28532009-06-17 22:02:04 +0000369 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000370 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
371 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000372
373 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000374 // Utility methods.
375 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek451ac092009-08-06 04:50:20 +0000377 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000378 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000379 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000380
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000381 void print(Store store, llvm::raw_ostream& Out, const char* nl,
382 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000383
384 void iterBindings(Store store, BindingsHandler& f) {
385 // FIXME: Implement.
386 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000387
Ted Kremenek67f28532009-06-17 22:02:04 +0000388 // FIXME: Remove.
389 BasicValueFactory& getBasicVals() {
390 return StateMgr.getBasicVals();
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek67f28532009-06-17 22:02:04 +0000393 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000394 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000395};
396
397} // end anonymous namespace
398
Ted Kremenek9af46f52009-06-16 22:36:44 +0000399//===----------------------------------------------------------------------===//
400// RegionStore creation.
401//===----------------------------------------------------------------------===//
402
403StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
404 RegionStoreFeatures F = maximal_features_tag();
405 return new RegionStoreManager(StMgr, F);
406}
407
408StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
409 RegionStoreFeatures F = minimal_features_tag();
410 F.enableFields(true);
411 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000412}
413
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414void
415RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000416 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000417 const MemRegion *superR = R->getSuperRegion();
418 if (add(superR, R))
419 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000420 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000421}
422
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000423RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000424RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
425 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000426 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428 llvm::SmallVector<const SubRegion*, 10> WL;
429
Ted Kremenek451ac092009-08-06 04:50:20 +0000430 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000431 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
432 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000435 // don't have direct bindings but are super regions of those that do.
436 while (!WL.empty()) {
437 const SubRegion *R = WL.back();
438 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000439 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000440 }
441
Ted Kremenek14453bf2009-03-03 19:02:42 +0000442 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000443}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000444
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000446 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447}
448
Ted Kremenek9af46f52009-06-16 22:36:44 +0000449//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000450// Binding invalidation.
451//===----------------------------------------------------------------------===//
452
Zhongxing Xu13d50172009-10-11 08:08:02 +0000453void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
454 const MemRegion *R,
455 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000456 RegionStoreSubRegionMap::iterator I, E;
457
458 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000459 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000461 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000462}
463
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000464const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
465 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000466 const Expr *Ex,
Ted Kremenek473e1672009-10-16 00:30:49 +0000467 unsigned Count,
468 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000469 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000471 // Strip away casts.
Zhongxing Xu479529e2009-11-10 02:17:20 +0000472 R = R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000473
Ted Kremenek87806792009-09-27 20:45:21 +0000474 // Get the mapping of regions -> subregions.
475 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000476 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000477
478 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000479
Ted Kremenek87806792009-09-27 20:45:21 +0000480 llvm::DenseMap<const MemRegion *, unsigned> Visited;
481 llvm::SmallVector<const MemRegion *, 10> WorkList;
482 WorkList.push_back(R);
483
484 while (!WorkList.empty()) {
485 R = WorkList.back();
486 WorkList.pop_back();
487
488 // Have we visited this region before?
489 unsigned &visited = Visited[R];
490 if (visited)
491 continue;
492 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek87806792009-09-27 20:45:21 +0000494 // Add subregions to work list.
495 RegionStoreSubRegionMap::iterator I, E;
496 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
497 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000498
499 // Get the old binding. Is it a region? If so, add it to the worklist.
500 if (Optional<SVal> V = getDirectBinding(B, R)) {
501 if (const MemRegion *RV = V->getAsRegion())
502 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000503
504 // A symbol? Mark it touched by the invalidation.
505 if (IS) {
506 if (SymbolRef Sym = V->getAsSymbol())
507 IS->insert(Sym);
508 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000509 }
510
Ted Kremenek473e1672009-10-16 00:30:49 +0000511 // Symbolic region? Mark that symbol touched by the invalidation.
512 if (IS) {
513 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
514 IS->insert(SR->getSymbol());
515 }
516
517 // Handle the region itself.
Ted Kremenek87806792009-09-27 20:45:21 +0000518 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
519 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000520 // Invalidate the region by setting its default value to
521 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000522 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
523 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000524 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000525 continue;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek87806792009-09-27 20:45:21 +0000528 if (!R->isBoundable())
529 continue;
530
531 const TypedRegion *TR = cast<TypedRegion>(R);
532 QualType T = TR->getValueType(Ctx);
533
534 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000535 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
536
Zhongxing Xu13d50172009-10-11 08:08:02 +0000537 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000538 if (!RD)
539 continue;
540
Zhongxing Xu13d50172009-10-11 08:08:02 +0000541 // Invalidate the region by setting its default value to
542 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000543 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
544 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000545 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000546 continue;
547 }
548
549 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
550 // Set the default value of the array to conjured symbol.
551 DefinedOrUnknownSVal V =
552 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000553 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000554 continue;
555 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000556
557 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
558 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000559 // For fields and elements whose super region has also been invalidated,
560 // only remove the old binding. The super region will get set with a
561 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000562 B = RBFactory.Remove(B, R);
563 continue;
564 }
Ted Kremenek87806792009-09-27 20:45:21 +0000565
Ted Kremenek389c44c2009-09-29 03:12:50 +0000566 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000567 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
568 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000569 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000570 }
571
Ted Kremenek87806792009-09-27 20:45:21 +0000572 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000573 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000574}
575
576//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000577// getLValueXXX methods.
578//===----------------------------------------------------------------------===//
579
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000580/// getLValueString - Returns an SVal representing the lvalue of a
581/// StringLiteral. Within RegionStore a StringLiteral has an
582/// associated StringRegion, and the lvalue of a StringLiteral is the
583/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000584SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000585 return loc::MemRegionVal(MRMgr.getStringRegion(S));
586}
587
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000588/// getLValueVar - Returns an SVal that represents the lvalue of a
589/// variable. Within RegionStore a variable has an associated
590/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000591SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000592 const LocationContext *LC) {
593 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000594}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000595
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000596/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
597/// of a compound literal. Within RegionStore a compound literal
598/// has an associated region, and the lvalue of the compound literal
599/// is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000600SVal
601RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000602 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
603}
604
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000605SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
606 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000607}
608
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000609SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
610 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000611}
612
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000613SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000614 if (Base.isUnknownOrUndef())
615 return Base;
616
617 Loc BaseL = cast<Loc>(Base);
618 const MemRegion* BaseR = 0;
619
620 switch (BaseL.getSubKind()) {
621 case loc::MemRegionKind:
622 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
623 break;
624
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000625 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000626 // These are anormal cases. Flag an undefined value.
627 return UndefinedVal();
628
629 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000630 // While these seem funny, this can happen through casts.
631 // FIXME: What we should return is the field offset. For example,
632 // add the field offset to the integer value. That way funny things
633 // like this work properly: &(((struct foo *) 0xa)->f)
634 return Base;
635
636 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000637 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000638 return Base;
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000641 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
642 // of FieldDecl.
643 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
644 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000645
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000646 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000647}
648
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000649SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
650 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000651
Ted Kremenekde7ec632009-03-09 22:44:49 +0000652 // If the base is an unknown or undefined value, just return it back.
653 // FIXME: For absolute pointer addresses, we just return that value back as
654 // well, although in reality we should return the offset added to that
655 // value.
656 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000657 return Base;
658
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000659 // Only handle integer offsets... for now.
660 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000661 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000662
Zhongxing Xuce760782009-05-09 13:20:07 +0000663 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000664
665 // Pointer of any type can be cast and used as array base.
666 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Ted Kremenek46537392009-07-16 01:33:37 +0000668 // Convert the offset to the appropriate size and signedness.
669 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000671 if (!ElemR) {
672 //
673 // If the base region is not an ElementRegion, create one.
674 // This can happen in the following example:
675 //
676 // char *p = __builtin_alloc(10);
677 // p[1] = 8;
678 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000679 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000680 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000681 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000682 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000685 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000687 if (!isa<nonloc::ConcreteInt>(BaseIdx))
688 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000690 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
691 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
692 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek46537392009-07-16 01:33:37 +0000694 // Compute the new index.
695 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek46537392009-07-16 01:33:37 +0000697 // Construct the new ElementRegion.
698 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000699 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000700 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000701}
702
Ted Kremenek9af46f52009-06-16 22:36:44 +0000703//===----------------------------------------------------------------------===//
704// Extents for regions.
705//===----------------------------------------------------------------------===//
706
Zhongxing Xue884ff82009-11-12 02:48:32 +0000707DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
708 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000710 switch (R->getKind()) {
711 case MemRegion::MemSpaceRegionKind:
712 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000713 return UnknownVal();
714
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000715 case MemRegion::CodeTextRegionKind:
716 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000717 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000718
719 // Not yet handled.
720 case MemRegion::AllocaRegionKind:
721 case MemRegion::CompoundLiteralRegionKind:
722 case MemRegion::ElementRegionKind:
723 case MemRegion::FieldRegionKind:
724 case MemRegion::ObjCIvarRegionKind:
725 case MemRegion::ObjCObjectRegionKind:
726 case MemRegion::SymbolicRegionKind:
727 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000729 case MemRegion::StringRegionKind: {
730 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000731 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000732 // operations with signed indices.
733 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000734 }
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000736 case MemRegion::VarRegionKind: {
737 const VarRegion* VR = cast<VarRegion>(R);
738 // Get the type of the variable.
739 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000741 // FIXME: Handle variable-length arrays.
742 if (isa<VariableArrayType>(T))
743 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000745 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
746 // return the size as signed integer.
747 return ValMgr.makeIntVal(CAT->getSize(), false);
748 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000749
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000750 // Clients can use ordinary variables as if they were arrays. These
751 // essentially are arrays of size 1.
752 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000755 case MemRegion::BEG_DECL_REGIONS:
756 case MemRegion::END_DECL_REGIONS:
757 case MemRegion::BEG_TYPED_REGIONS:
758 case MemRegion::END_TYPED_REGIONS:
759 assert(0 && "Infeasible region");
760 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000761 }
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000763 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000764 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000765}
766
Ted Kremenek67f28532009-06-17 22:02:04 +0000767const GRState *RegionStoreManager::setExtent(const GRState *state,
768 const MemRegion *region,
769 SVal extent) {
770 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000771}
772
773//===----------------------------------------------------------------------===//
774// Location and region casting.
775//===----------------------------------------------------------------------===//
776
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000777/// ArrayToPointer - Emulates the "decay" of an array to a pointer
778/// type. 'Array' represents the lvalue of the array being decayed
779/// to a pointer, and the returned SVal represents the decayed
780/// version of that lvalue (i.e., a pointer to the first element of
781/// the array). This is called by GRExprEngine when evaluating casts
782/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000783SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000784 if (!isa<loc::MemRegionVal>(Array))
785 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenekabb042f2008-12-13 19:24:37 +0000787 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
788 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000790 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000791 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000793 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000794 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000795 ArrayType *AT = cast<ArrayType>(T);
796 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Ted Kremenek75185b52009-07-16 00:00:11 +0000798 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
799 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000800
801 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000802}
803
Ted Kremenek9af46f52009-06-16 22:36:44 +0000804//===----------------------------------------------------------------------===//
805// Pointer arithmetic.
806//===----------------------------------------------------------------------===//
807
Mike Stump1eb44332009-09-09 15:08:12 +0000808SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000809 BinaryOperator::Opcode Op, Loc L, NonLoc R,
810 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000811 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000812 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000813 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000814
Zhongxing Xua1718c72009-04-03 07:33:13 +0000815 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000816 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000817
Ted Kremenek3bccf082009-07-11 00:58:27 +0000818 switch (MR->getKind()) {
819 case MemRegion::SymbolicRegionKind: {
820 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000821 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000822 QualType T = Sym->getType(getContext());
823 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000825 if (const PointerType *PT = T->getAs<PointerType>())
826 EleTy = PT->getPointeeType();
827 else
John McCall183700f2009-09-21 23:43:11 +0000828 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Ted Kremenek3bccf082009-07-11 00:58:27 +0000830 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
831 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000832 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000833 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000834 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000835 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000836 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000837 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000838 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
839 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000840 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000842
Ted Kremenek3bccf082009-07-11 00:58:27 +0000843 case MemRegion::ElementRegionKind: {
844 ER = cast<ElementRegion>(MR);
845 break;
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenek3bccf082009-07-11 00:58:27 +0000848 // Not yet handled.
849 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000850 case MemRegion::StringRegionKind: {
851
852 }
853 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000854 case MemRegion::CompoundLiteralRegionKind:
855 case MemRegion::FieldRegionKind:
856 case MemRegion::ObjCObjectRegionKind:
857 case MemRegion::ObjCIvarRegionKind:
858 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Ted Kremenek3bccf082009-07-11 00:58:27 +0000860 case MemRegion::CodeTextRegionKind:
861 // Technically this can happen if people do funny things with casts.
862 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Ted Kremenek3bccf082009-07-11 00:58:27 +0000864 case MemRegion::MemSpaceRegionKind:
865 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
866 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek3bccf082009-07-11 00:58:27 +0000868 case MemRegion::BEG_DECL_REGIONS:
869 case MemRegion::END_DECL_REGIONS:
870 case MemRegion::BEG_TYPED_REGIONS:
871 case MemRegion::END_TYPED_REGIONS:
872 assert(0 && "Infeasible region");
873 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000874 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000875
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000876 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000877 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000878
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000879 // For now, only support:
880 // (a) concrete integer indices that can easily be resolved
881 // (b) 0 + symbolic index
882 if (Base) {
883 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
884 // FIXME: Should use SValuator here.
885 SVal NewIdx =
886 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000887 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000888 const MemRegion* NewER =
889 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
890 ER->getSuperRegion(), getContext());
891 return ValMgr.makeLoc(NewER);
892 }
893 if (0 == Base->getValue()) {
894 const MemRegion* NewER =
895 MRMgr.getElementRegion(ER->getElementType(), R,
896 ER->getSuperRegion(), getContext());
897 return ValMgr.makeLoc(NewER);
898 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Ted Kremenek5dc27462009-03-03 02:51:43 +0000901 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000902}
903
Ted Kremenek9af46f52009-06-16 22:36:44 +0000904//===----------------------------------------------------------------------===//
905// Loading values from regions.
906//===----------------------------------------------------------------------===//
907
Zhongxing Xu13d50172009-10-11 08:08:02 +0000908Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
909 const MemRegion *R) {
910 if (const BindingVal *BV = B.lookup(R))
911 return Optional<SVal>::create(BV->getDirectValue());
912
913 return Optional<SVal>();
914}
915
916Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000917 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000919 if (R->isBoundable())
920 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
921 if (TR->getValueType(getContext())->isUnionType())
922 return UnknownVal();
923
Zhongxing Xu13d50172009-10-11 08:08:02 +0000924 if (BindingVal const *V = B.lookup(R))
925 return Optional<SVal>::create(V->getDefaultValue());
926
927 return Optional<SVal>();
928}
929
930Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
931 const MemRegion *R) {
932 if (const BindingVal *BV = B.lookup(R))
933 return Optional<SVal>::create(BV->getValue());
934
935 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000936}
937
Ted Kremeneka6275a52009-07-15 02:31:43 +0000938static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
939 RTy = Ctx.getCanonicalType(RTy);
940 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Ted Kremeneka6275a52009-07-15 02:31:43 +0000942 if (RTy == UsedTy)
943 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
945
Ted Kremenek25c54572009-07-20 22:58:02 +0000946 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000947 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000948 // represents a reinterpretation.
949 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000950 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000951 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000952
953 return PUsedTy && PRTy &&
954 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000955 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000956 }
957
958 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000959}
960
Ted Kremenek0954cde2009-09-24 04:11:44 +0000961const ElementRegion *
962RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
963 ASTContext &Ctx = getContext();
964 SVal idx = ValMgr.makeZeroArrayIndex();
965 assert(!T.isNull());
966 return MRMgr.getElementRegion(T, idx, SR, Ctx);
967}
968
969
970
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000971SValuator::CastResult
972RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000973
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000974 assert(!isa<UnknownVal>(L) && "location unknown");
975 assert(!isa<UndefinedVal>(L) && "location undefined");
976
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000977 // FIXME: Is this even possible? Shouldn't this be treated as a null
978 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000979 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000980 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000981
Ted Kremenek67f28532009-06-17 22:02:04 +0000982 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000983
Zhongxing Xu91844122009-05-20 09:18:48 +0000984 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000985 // Example:
986 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000987 // char* p = alloca();
988 // read(p);
989 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000990 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000991 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Ted Kremenek0954cde2009-09-24 04:11:44 +0000993 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
994 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek968f0a62009-08-03 21:41:46 +0000996 if (isa<CodeTextRegion>(MR))
997 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000999 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1000 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001001 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001002 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001003
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001004 // FIXME: We should eventually handle funny addressing. e.g.:
1005 //
1006 // int x = ...;
1007 // int *p = &x;
1008 // char *q = (char*) p;
1009 // char c = *q; // returns the first byte of 'x'.
1010 //
1011 // Such funny addressing will occur due to layering of regions.
1012
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001013#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001014 ASTContext &Ctx = getContext();
1015 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001016 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1017 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001018 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001019 assert(Ctx.getCanonicalType(RTy) ==
1020 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001021 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001022#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001023
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001024 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001025 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001027 // FIXME: Handle unions.
1028 if (RTy->isUnionType())
1029 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001030
1031 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001032 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001033
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001034 // FIXME: handle Vector types.
1035 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001036 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001037
1038 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001039 return SValuator::CastResult(state,
1040 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001041
1042 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001043 return SValuator::CastResult(state,
1044 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Ted Kremenek25c54572009-07-20 22:58:02 +00001046 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001047 return SValuator::CastResult(state,
1048 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek9031dd72009-07-21 00:12:07 +00001050 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001051 return SValuator::CastResult(state,
1052 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001053
Ted Kremenek451ac092009-08-06 04:50:20 +00001054 RegionBindings B = GetRegionBindings(state->getStore());
1055 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001056
1057 // Check if the region has a binding.
1058 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001059 if (SVal const *SV = V->getValue())
1060 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001061
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001062 // The location does not have a bound value. This means that it has
1063 // the value it had upon its creation and/or entry to the analyzed
1064 // function/method. These are either symbolic values or 'undefined'.
1065
Ted Kremenek356e9d62009-07-22 04:35:42 +00001066#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001067 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001068#else
1069 if (R->hasStackStorage()) {
1070#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001071 // All stack variables are considered to have undefined values
1072 // upon creation. All heap allocated blocks are considered to
1073 // have undefined values as well unless they are explicitly bound
1074 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001075 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001076 }
1077
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001078 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001079 return SValuator::CastResult(state,
1080 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001081}
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001083std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001084RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001085 if (Optional<SVal> OV = getDirectBinding(B, R))
1086 if (const nonloc::LazyCompoundVal *V =
1087 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1088 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001090 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1091 const std::pair<const GRState *, const MemRegion *> &X =
1092 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001094 if (X.first)
1095 return std::make_pair(X.first,
1096 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001097 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001098 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1099 const std::pair<const GRState *, const MemRegion *> &X =
1100 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001102 if (X.first)
1103 return std::make_pair(X.first,
1104 MRMgr.getFieldRegionWithSuper(FR, X.second));
1105 }
1106
1107 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1108}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001109
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001110SVal RegionStoreManager::RetrieveElement(const GRState* state,
1111 const ElementRegion* R) {
1112 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001113 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001114 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001115 return *V;
1116
Ted Kremenek921109a2009-07-01 23:19:52 +00001117 const MemRegion* superR = R->getSuperRegion();
1118
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001119 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001120 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001121 // FIXME: Handle loads from strings where the literal is treated as
1122 // an integer, e.g., *((unsigned int*)"hello")
1123 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001124 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001125 if (T != Ctx.getCanonicalType(R->getElementType()))
1126 return UnknownVal();
1127
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001128 const StringLiteral *Str = StrR->getStringLiteral();
1129 SVal Idx = R->getIndex();
1130 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1131 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001132 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001133 if (i > byteLength) {
1134 // Buffer overflow checking in GRExprEngine should handle this case,
1135 // but we shouldn't rely on it to not overflow here if that checking
1136 // is disabled.
1137 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001138 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001139 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001140 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001141 }
1142 }
Mike Stump1eb44332009-09-09 15:08:12 +00001143
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001144 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001145 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001146 if (SymbolRef parentSym = V->getAsSymbol())
1147 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001148
1149 if (V->isUnknownOrUndef())
1150 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001151
1152 // Handle LazyCompoundVals for the immediate super region. Other cases
1153 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001154 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001155 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001157 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1158 return RetrieveElement(LCV->getState(), R);
1159 }
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremeneka6275a52009-07-15 02:31:43 +00001161 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001162 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001163 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001164
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001165 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001166}
1167
Mike Stump1eb44332009-09-09 15:08:12 +00001168SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001169 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001170
1171 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001172 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001173 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001174 return *V;
1175
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001176 QualType Ty = R->getValueType(getContext());
1177 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1178}
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001180SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1181 const TypedRegion *R,
1182 QualType Ty,
1183 const MemRegion *superR) {
1184
Mike Stump1eb44332009-09-09 15:08:12 +00001185 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001186 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001188 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001190 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001191 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001192 if (SymbolRef parentSym = D->getAsSymbol())
1193 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001195 if (D->isZeroConstant())
1196 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001198 if (D->isUnknown())
1199 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001201 assert(0 && "Unknown default value");
1202 }
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001204 // If our super region is a field or element itself, walk up the region
1205 // hierarchy to see if there is a default value installed in an ancestor.
1206 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1207 superR = cast<SubRegion>(superR)->getSuperRegion();
1208 continue;
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001211 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001214 // Lazy binding?
1215 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001216 const MemRegion *lazyBindingRegion = NULL;
1217 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001219 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001220 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001222 if (isa<ElementRegion>(R))
1223 return RetrieveElement(lazyBindingState,
1224 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001226 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001227 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001228 }
1229
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001230 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001232 if (isa<ElementRegion>(R)) {
1233 // Currently we don't reason specially about Clang-style vectors. Check
1234 // if superR is a vector and if so return Unknown.
1235 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1236 if (typedSuperR->getValueType(getContext())->isVectorType())
1237 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001238 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001239 }
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001241 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001244 // All other values are symbolic.
1245 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001246}
Mike Stump1eb44332009-09-09 15:08:12 +00001247
1248SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001249 const ObjCIvarRegion* R) {
1250
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001251 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001252 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001253
Zhongxing Xu13d50172009-10-11 08:08:02 +00001254 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001255 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001257 const MemRegion *superR = R->getSuperRegion();
1258
Ted Kremenekab22ee92009-10-20 01:20:57 +00001259 // Check if the super region has a default binding.
1260 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001261 if (SymbolRef parentSym = V->getAsSymbol())
1262 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001264 // Other cases: give up.
1265 return UnknownVal();
1266 }
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek25c54572009-07-20 22:58:02 +00001268 return RetrieveLazySymbol(state, R);
1269}
1270
Ted Kremenek9031dd72009-07-21 00:12:07 +00001271SVal RegionStoreManager::RetrieveVar(const GRState *state,
1272 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001273
Ted Kremenek9031dd72009-07-21 00:12:07 +00001274 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001275 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Zhongxing Xu13d50172009-10-11 08:08:02 +00001277 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001278 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek9031dd72009-07-21 00:12:07 +00001280 // Lazily derive a value for the VarRegion.
1281 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283 if (R->hasGlobalsOrParametersStorage())
1284 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenek9031dd72009-07-21 00:12:07 +00001286 return UndefinedVal();
1287}
1288
Mike Stump1eb44332009-09-09 15:08:12 +00001289SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001290 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek25c54572009-07-20 22:58:02 +00001292 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001293
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001294 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001295 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001296}
1297
Mike Stump1eb44332009-09-09 15:08:12 +00001298SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1299 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001300 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001301 assert(T->isStructureType());
1302
Zhongxing Xub7507d12009-06-11 07:27:30 +00001303 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001304 RecordDecl* RD = RT->getDecl();
1305 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001306 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001307#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001308 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1309
Ted Kremenek67f28532009-06-17 22:02:04 +00001310 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1311 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001312 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001313
Douglas Gregore267ff32008-12-11 20:41:00 +00001314 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1315 FieldEnd = Fields.rend();
1316 Field != FieldEnd; ++Field) {
1317 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001318 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001319 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001320 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1321 }
1322
Zhongxing Xud91ee272009-06-23 09:02:15 +00001323 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001324#else
1325 return ValMgr.makeLazyCompoundVal(state, R);
1326#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001327}
1328
Ted Kremenek67f28532009-06-17 22:02:04 +00001329SVal RegionStoreManager::RetrieveArray(const GRState *state,
1330 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001331#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001332 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001333 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1334
1335 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001336 uint64_t size = CAT->getSize().getZExtValue();
1337 for (uint64_t i = 0; i < size; ++i) {
1338 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001339 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001340 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001341 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001342 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001343 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1344 }
1345
Zhongxing Xud91ee272009-06-23 09:02:15 +00001346 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001347#else
1348 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1349 return ValMgr.makeLazyCompoundVal(state, R);
1350#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001351}
1352
Ted Kremenek9af46f52009-06-16 22:36:44 +00001353//===----------------------------------------------------------------------===//
1354// Binding values to regions.
1355//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001356
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001357Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001358 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001359
Ted Kremenek0964a062009-01-21 06:57:53 +00001360 if (isa<loc::MemRegionVal>(L))
1361 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Ted Kremenek0964a062009-01-21 06:57:53 +00001363 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001364 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001365 return RBFactory.Remove(B, R).getRoot();
1366 }
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Ted Kremenek0964a062009-01-21 06:57:53 +00001368 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001369}
1370
Ted Kremenek67f28532009-06-17 22:02:04 +00001371const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001372 if (isa<loc::ConcreteInt>(L))
1373 return state;
1374
Ted Kremenek9af46f52009-06-16 22:36:44 +00001375 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001376 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378 // Check if the region is a struct region.
1379 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1380 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001381 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001383 // Special case: the current region represents a cast and it and the super
1384 // region both have pointer types or intptr_t types. If so, perform the
1385 // bind to the super region.
1386 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001387 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001388 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1389 if (ER->getIndex().isZeroConstant()) {
1390 if (const TypedRegion *superR =
1391 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1392 ASTContext &Ctx = getContext();
1393 QualType superTy = superR->getValueType(Ctx);
1394 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001395
1396 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001397 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001398 SValuator::CastResult cr =
1399 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001400 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1401 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001402 // For now, just invalidate the fields of the struct/union/class.
1403 // FIXME: Precisely handle the fields of the record.
1404 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001405 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001406 }
1407 }
1408 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001409 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1410 // Binding directly to a symbolic region should be treated as binding
1411 // to element 0.
1412 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001413 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001414 R = GetElementZeroRegion(SR, T);
1415 }
Mike Stump1eb44332009-09-09 15:08:12 +00001416
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001417 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001418 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001419 return state->makeWithStore(
1420 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001421}
1422
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001423const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001424 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001425 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001426
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001427 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001428
Ted Kremenek0964a062009-01-21 06:57:53 +00001429 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001430 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001431 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001432 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001433
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001434 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001435}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001436
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001437// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001438const GRState *
1439RegionStoreManager::BindCompoundLiteral(const GRState *state,
1440 const CompoundLiteralExpr* CL,
1441 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001442
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001443 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001444 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001445}
1446
Ted Kremenek027e2662009-11-19 20:20:24 +00001447const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1448 const MemRegion *R,
1449 QualType T) {
1450 Store store = state->getStore();
1451 RegionBindings B = GetRegionBindings(store);
1452 SVal V;
1453
1454 if (Loc::IsLocType(T))
1455 V = ValMgr.makeNull();
1456 else if (T->isIntegerType())
1457 V = ValMgr.makeZeroVal(T);
1458 else if (T->isStructureType() || T->isArrayType()) {
1459 // Set the default value to a zero constant when it is a structure
1460 // or array. The type doesn't really matter.
1461 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1462 }
1463 else {
1464 return state;
1465 }
1466
1467 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1468 return state->makeWithStore(B.getRoot());
1469}
1470
Ted Kremenek67f28532009-06-17 22:02:04 +00001471const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001472 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001473 SVal Init) {
1474
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001475 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001476 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001477 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001478
Ted Kremenek46537392009-07-16 01:33:37 +00001479 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001480
1481 // Check if the init expr is a StringLiteral.
1482 if (isa<loc::MemRegionVal>(Init)) {
1483 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1484 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1485 const char* str = S->getStrData();
1486 unsigned len = S->getByteLength();
1487 unsigned j = 0;
1488
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001489 // Copy bytes from the string literal into the target array. Trailing bytes
1490 // in the array that are not covered by the string literal are initialized
1491 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001492 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001493 if (j >= len)
1494 break;
1495
Ted Kremenek46537392009-07-16 01:33:37 +00001496 SVal Idx = ValMgr.makeArrayIndex(i);
1497 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1498 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001499
Zhongxing Xud91ee272009-06-23 09:02:15 +00001500 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001501 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001502 }
1503
Ted Kremenek67f28532009-06-17 22:02:04 +00001504 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001505 }
1506
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001507 // Handle lazy compound values.
1508 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1509 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001510
1511 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001512
1513 if (Init.isUnknown())
1514 return setImplicitDefaultValue(state, R, ElementTy);
1515
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001516 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001517 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001518 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Ted Kremenek46537392009-07-16 01:33:37 +00001520 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001521 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001522 if (VI == VE)
1523 break;
1524
Ted Kremenek46537392009-07-16 01:33:37 +00001525 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001526 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001527
1528 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001529 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001530 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001531 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001532 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001533 }
1534
Ted Kremenek027e2662009-11-19 20:20:24 +00001535 // If the init list is shorter than the array length, set the
1536 // array default value.
1537 if (i < size)
1538 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001539
Ted Kremenek67f28532009-06-17 22:02:04 +00001540 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001541}
1542
Ted Kremenek67f28532009-06-17 22:02:04 +00001543const GRState *
1544RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1545 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Ted Kremenek67f28532009-06-17 22:02:04 +00001547 if (!Features.supportsFields())
1548 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001550 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001551 assert(T->isStructureType());
1552
Ted Kremenek6217b802009-07-29 21:53:49 +00001553 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001554 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001555
1556 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001557 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001558
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001559 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001560 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001561 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Ted Kremenek67f28532009-06-17 22:02:04 +00001563 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1564 // Ignore them and kill the field values.
1565 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001566 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001567
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001568 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001569 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001570
1571 RecordDecl::field_iterator FI, FE;
1572
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001573 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001574
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001575 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001576 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001577
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001578 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001579 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001580
Ted Kremenekcf549592009-09-22 21:19:14 +00001581 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001582 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001583 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001584 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001585 else
1586 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001587 }
1588
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001589 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001590 if (FI != FE) {
1591 Store store = state->getStore();
1592 RegionBindings B = GetRegionBindings(store);
1593 B = RBFactory.Add(B, R,
1594 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1595 state = state->makeWithStore(B.getRoot());
1596 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001597
Ted Kremenek67f28532009-06-17 22:02:04 +00001598 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001599}
1600
Zhongxing Xu13d50172009-10-11 08:08:02 +00001601Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1602 RegionBindings B = GetRegionBindings(store);
1603 llvm::OwningPtr<RegionStoreSubRegionMap>
1604 SubRegions(getRegionStoreSubRegionMap(store));
1605 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001606
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001607 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001608 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001609
Zhongxing Xu13d50172009-10-11 08:08:02 +00001610 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001611}
1612
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001613const GRState*
1614RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1615 const GRState *state,
1616 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001617
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001618 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001619 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001620
Mike Stump1eb44332009-09-09 15:08:12 +00001621 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001622 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001623
Mike Stump1eb44332009-09-09 15:08:12 +00001624 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001625 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001627 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1628 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001629 return state->makeWithStore(
1630 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001631}
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Ted Kremenek9af46f52009-06-16 22:36:44 +00001633//===----------------------------------------------------------------------===//
1634// State pruning.
1635//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001636
Mike Stump1eb44332009-09-09 15:08:12 +00001637void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001638 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001639 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001640{
Ted Kremenek781115c2009-10-17 17:45:11 +00001641 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1642
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001643 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001644 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Ted Kremenek9af46f52009-06-16 22:36:44 +00001646 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001647 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001648 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001649
1650 // Do a pass over the regions in the store. For VarRegions we check if
1651 // the variable is still live and if so add it to the list of live roots.
1652 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001653 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001654
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001655 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001656 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001657 const MemRegion *R = I.getKey();
1658 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001659 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001660
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001661 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001662 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001663 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001664 llvm::SmallVector<RBDNode, 10> Postponed;
1665
Zhongxing Xu6800b332009-10-18 04:15:47 +00001666 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001667
Ted Kremenek9af46f52009-06-16 22:36:44 +00001668 while (!IntermediateRoots.empty()) {
1669 const MemRegion* R = IntermediateRoots.back();
1670 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001671
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001672 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001673 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001674 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001675
Ted Kremenek9af46f52009-06-16 22:36:44 +00001676 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001677 if (SymReaper.isLive(Loc, VR->getDecl()))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001678 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001679 continue;
1680 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001681
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001683 llvm::SmallVectorImpl<RBDNode> &Q =
1684 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1685
1686 Q.push_back(std::make_pair(&state, SR));
1687
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001688 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001689 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001690
1691 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001692 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001693 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001694 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001695 }
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001697 // Enqueue the RegionRoots onto WorkList.
1698 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1699 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001700 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001701 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001702 RegionRoots.clear();
1703
Zhongxing Xu6800b332009-10-18 04:15:47 +00001704 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001705
Ted Kremenek01756192009-10-29 05:14:17 +00001706tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001707 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001708 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001709 WorkList.pop_back();
1710
1711 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001712 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001713 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001714 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001716 const MemRegion *R = N.second;
1717 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001718
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001719 // Enqueue subregions.
1720 RegionStoreSubRegionMap *M;
1721
1722 if (&state == state_N)
1723 M = SubRegions.get();
1724 else {
1725 RegionStoreSubRegionMap *& SM = SC[state_N];
1726 if (!SM)
1727 SM = getRegionStoreSubRegionMap(state_N->getStore());
1728 M = SM;
1729 }
1730
1731 RegionStoreSubRegionMap::iterator I, E;
1732 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1733 WorkList.push_back(std::make_pair(state_N, *I));
1734
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001735 // Enqueue the super region.
1736 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1737 const MemRegion *superR = SR->getSuperRegion();
1738 if (!isa<MemSpaceRegion>(superR)) {
1739 // If 'R' is a field or an element, we want to keep the bindings
1740 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001741 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001742 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1743 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001744 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001745 }
1746 }
1747
1748 // Mark the symbol for any live SymbolicRegion as "live". This means we
1749 // should continue to track that symbol.
1750 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1751 SymReaper.markLive(SymR->getSymbol());
1752
1753 Store store_N = state_N->getStore();
1754 RegionBindings B_N = GetRegionBindings(store_N);
1755
1756 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001757 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001758
Zhongxing Xu13d50172009-10-11 08:08:02 +00001759 if (V) {
1760 // Check for lazy bindings.
1761 if (const nonloc::LazyCompoundVal *LCV =
1762 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001763
Zhongxing Xu13d50172009-10-11 08:08:02 +00001764 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001765 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001766 }
1767 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001768 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001769 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001770 SI!=SE;++SI)
1771 SymReaper.markLive(*SI);
1772
Zhongxing Xu13d50172009-10-11 08:08:02 +00001773 // If V is a region, then add it to the worklist.
1774 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001775 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001776 }
1777 }
1778 }
1779
Ted Kremenek01756192009-10-29 05:14:17 +00001780 // See if any postponed SymbolicRegions are actually live now, after
1781 // having done a scan.
1782 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1783 E = Postponed.end() ; I != E ; ++I) {
1784 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1785 if (SymReaper.isLive(SR->getSymbol())) {
1786 WorkList.push_back(*I);
1787 I->second = NULL;
1788 }
1789 }
1790 }
1791
1792 if (!WorkList.empty())
1793 goto tryAgain;
1794
Ted Kremenek9af46f52009-06-16 22:36:44 +00001795 // We have now scanned the store, marking reachable regions and symbols
1796 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001797 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001798 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001799 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001800 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001801 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001802 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001803
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001805 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Ted Kremenek9af46f52009-06-16 22:36:44 +00001807 // Mark all non-live symbols that this region references as dead.
1808 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1809 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Zhongxing Xu13d50172009-10-11 08:08:02 +00001811 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001812 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1813 for (; SI != SE; ++SI)
1814 SymReaper.maybeDead(*SI);
1815 }
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001817 // Write the store back.
1818 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819}
1820
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001821GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1822 StackFrameContext const *frame) {
1823 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1824 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1825
1826 FunctionDecl::param_const_iterator PI = FD->param_begin();
1827
1828 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1829
1830 // Copy the arg expression value to the arg variables.
1831 for (; AI != AE; ++AI, ++PI) {
1832 SVal ArgVal = state->getSVal(*AI);
1833 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1834 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1835 }
1836
1837 return state;
1838}
1839
Ted Kremenek9af46f52009-06-16 22:36:44 +00001840//===----------------------------------------------------------------------===//
1841// Utility methods.
1842//===----------------------------------------------------------------------===//
1843
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001844void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001845 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001846 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001847 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Ted Kremenek451ac092009-08-06 04:50:20 +00001849 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001850 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001851}