blob: 16a4690f95ad1ce44df11e471e60b18ea88caf3a [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 Kremenekeb1c7a02009-11-25 01:32:22 +0000715 case MemRegion::FunctionTextRegionKind:
716 case MemRegion::BlockTextRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000718 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000719
720 // Not yet handled.
721 case MemRegion::AllocaRegionKind:
722 case MemRegion::CompoundLiteralRegionKind:
723 case MemRegion::ElementRegionKind:
724 case MemRegion::FieldRegionKind:
725 case MemRegion::ObjCIvarRegionKind:
726 case MemRegion::ObjCObjectRegionKind:
727 case MemRegion::SymbolicRegionKind:
728 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000730 case MemRegion::StringRegionKind: {
731 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000732 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000733 // operations with signed indices.
734 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000737 case MemRegion::VarRegionKind: {
738 const VarRegion* VR = cast<VarRegion>(R);
739 // Get the type of the variable.
740 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000742 // FIXME: Handle variable-length arrays.
743 if (isa<VariableArrayType>(T))
744 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000746 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
747 // return the size as signed integer.
748 return ValMgr.makeIntVal(CAT->getSize(), false);
749 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000750
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000751 // Clients can use ordinary variables as if they were arrays. These
752 // essentially are arrays of size 1.
753 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000756 case MemRegion::BEG_DECL_REGIONS:
757 case MemRegion::END_DECL_REGIONS:
758 case MemRegion::BEG_TYPED_REGIONS:
759 case MemRegion::END_TYPED_REGIONS:
760 assert(0 && "Infeasible region");
761 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000764 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000765 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000766}
767
Ted Kremenek67f28532009-06-17 22:02:04 +0000768const GRState *RegionStoreManager::setExtent(const GRState *state,
769 const MemRegion *region,
770 SVal extent) {
771 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000772}
773
774//===----------------------------------------------------------------------===//
775// Location and region casting.
776//===----------------------------------------------------------------------===//
777
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000778/// ArrayToPointer - Emulates the "decay" of an array to a pointer
779/// type. 'Array' represents the lvalue of the array being decayed
780/// to a pointer, and the returned SVal represents the decayed
781/// version of that lvalue (i.e., a pointer to the first element of
782/// the array). This is called by GRExprEngine when evaluating casts
783/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000784SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000785 if (!isa<loc::MemRegionVal>(Array))
786 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenekabb042f2008-12-13 19:24:37 +0000788 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
789 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000791 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000792 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000794 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000795 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000796 ArrayType *AT = cast<ArrayType>(T);
797 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek75185b52009-07-16 00:00:11 +0000799 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
800 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000801
802 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000803}
804
Ted Kremenek9af46f52009-06-16 22:36:44 +0000805//===----------------------------------------------------------------------===//
806// Pointer arithmetic.
807//===----------------------------------------------------------------------===//
808
Mike Stump1eb44332009-09-09 15:08:12 +0000809SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000810 BinaryOperator::Opcode Op, Loc L, NonLoc R,
811 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000812 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000813 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000814 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000815
Zhongxing Xua1718c72009-04-03 07:33:13 +0000816 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000817 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000818
Ted Kremenek3bccf082009-07-11 00:58:27 +0000819 switch (MR->getKind()) {
820 case MemRegion::SymbolicRegionKind: {
821 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000822 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000823 QualType T = Sym->getType(getContext());
824 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000826 if (const PointerType *PT = T->getAs<PointerType>())
827 EleTy = PT->getPointeeType();
828 else
John McCall183700f2009-09-21 23:43:11 +0000829 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek3bccf082009-07-11 00:58:27 +0000831 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
832 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000833 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000834 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000835 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000837 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000838 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000839 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
840 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000841 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000842 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000843
Ted Kremenek3bccf082009-07-11 00:58:27 +0000844 case MemRegion::ElementRegionKind: {
845 ER = cast<ElementRegion>(MR);
846 break;
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 // Not yet handled.
850 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000851 case MemRegion::StringRegionKind: {
852
853 }
854 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000855 case MemRegion::CompoundLiteralRegionKind:
856 case MemRegion::FieldRegionKind:
857 case MemRegion::ObjCObjectRegionKind:
858 case MemRegion::ObjCIvarRegionKind:
859 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000861 case MemRegion::FunctionTextRegionKind:
862 case MemRegion::BlockTextRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000863 // Technically this can happen if people do funny things with casts.
864 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Ted Kremenek3bccf082009-07-11 00:58:27 +0000866 case MemRegion::MemSpaceRegionKind:
867 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
868 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Ted Kremenek3bccf082009-07-11 00:58:27 +0000870 case MemRegion::BEG_DECL_REGIONS:
871 case MemRegion::END_DECL_REGIONS:
872 case MemRegion::BEG_TYPED_REGIONS:
873 case MemRegion::END_TYPED_REGIONS:
874 assert(0 && "Infeasible region");
875 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000876 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000877
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000878 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000879 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000880
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000881 // For now, only support:
882 // (a) concrete integer indices that can easily be resolved
883 // (b) 0 + symbolic index
884 if (Base) {
885 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
886 // FIXME: Should use SValuator here.
887 SVal NewIdx =
888 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000889 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000890 const MemRegion* NewER =
891 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
892 ER->getSuperRegion(), getContext());
893 return ValMgr.makeLoc(NewER);
894 }
895 if (0 == Base->getValue()) {
896 const MemRegion* NewER =
897 MRMgr.getElementRegion(ER->getElementType(), R,
898 ER->getSuperRegion(), getContext());
899 return ValMgr.makeLoc(NewER);
900 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Ted Kremenek5dc27462009-03-03 02:51:43 +0000903 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000904}
905
Ted Kremenek9af46f52009-06-16 22:36:44 +0000906//===----------------------------------------------------------------------===//
907// Loading values from regions.
908//===----------------------------------------------------------------------===//
909
Zhongxing Xu13d50172009-10-11 08:08:02 +0000910Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
911 const MemRegion *R) {
912 if (const BindingVal *BV = B.lookup(R))
913 return Optional<SVal>::create(BV->getDirectValue());
914
915 return Optional<SVal>();
916}
917
918Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000919 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000921 if (R->isBoundable())
922 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
923 if (TR->getValueType(getContext())->isUnionType())
924 return UnknownVal();
925
Zhongxing Xu13d50172009-10-11 08:08:02 +0000926 if (BindingVal const *V = B.lookup(R))
927 return Optional<SVal>::create(V->getDefaultValue());
928
929 return Optional<SVal>();
930}
931
932Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
933 const MemRegion *R) {
934 if (const BindingVal *BV = B.lookup(R))
935 return Optional<SVal>::create(BV->getValue());
936
937 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000938}
939
Ted Kremeneka6275a52009-07-15 02:31:43 +0000940static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
941 RTy = Ctx.getCanonicalType(RTy);
942 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremeneka6275a52009-07-15 02:31:43 +0000944 if (RTy == UsedTy)
945 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
947
Ted Kremenek25c54572009-07-20 22:58:02 +0000948 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000949 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000950 // represents a reinterpretation.
951 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000952 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000953 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000954
955 return PUsedTy && PRTy &&
956 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000957 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000958 }
959
960 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000961}
962
Ted Kremenek0954cde2009-09-24 04:11:44 +0000963const ElementRegion *
964RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
965 ASTContext &Ctx = getContext();
966 SVal idx = ValMgr.makeZeroArrayIndex();
967 assert(!T.isNull());
968 return MRMgr.getElementRegion(T, idx, SR, Ctx);
969}
970
971
972
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000973SValuator::CastResult
974RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000975
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000976 assert(!isa<UnknownVal>(L) && "location unknown");
977 assert(!isa<UndefinedVal>(L) && "location undefined");
978
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000979 // FIXME: Is this even possible? Shouldn't this be treated as a null
980 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000981 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000982 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000983
Ted Kremenek67f28532009-06-17 22:02:04 +0000984 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000985
Zhongxing Xu91844122009-05-20 09:18:48 +0000986 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000987 // Example:
988 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000989 // char* p = alloca();
990 // read(p);
991 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000992 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000993 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremenek0954cde2009-09-24 04:11:44 +0000995 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
996 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Ted Kremenek968f0a62009-08-03 21:41:46 +0000998 if (isa<CodeTextRegion>(MR))
999 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001001 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1002 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001003 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001004 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001005
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001006 // FIXME: We should eventually handle funny addressing. e.g.:
1007 //
1008 // int x = ...;
1009 // int *p = &x;
1010 // char *q = (char*) p;
1011 // char c = *q; // returns the first byte of 'x'.
1012 //
1013 // Such funny addressing will occur due to layering of regions.
1014
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001015#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001016 ASTContext &Ctx = getContext();
1017 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001018 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1019 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001020 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001021 assert(Ctx.getCanonicalType(RTy) ==
1022 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001023 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001024#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001025
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001026 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001027 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001029 // FIXME: Handle unions.
1030 if (RTy->isUnionType())
1031 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001032
1033 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001034 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001035
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001036 // FIXME: handle Vector types.
1037 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001038 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001039
1040 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001041 return SValuator::CastResult(state,
1042 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001043
1044 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001045 return SValuator::CastResult(state,
1046 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Ted Kremenek25c54572009-07-20 22:58:02 +00001048 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001049 return SValuator::CastResult(state,
1050 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenek9031dd72009-07-21 00:12:07 +00001052 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001053 return SValuator::CastResult(state,
1054 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001055
Ted Kremenek451ac092009-08-06 04:50:20 +00001056 RegionBindings B = GetRegionBindings(state->getStore());
1057 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001058
1059 // Check if the region has a binding.
1060 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001061 if (SVal const *SV = V->getValue())
1062 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001063
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001064 // The location does not have a bound value. This means that it has
1065 // the value it had upon its creation and/or entry to the analyzed
1066 // function/method. These are either symbolic values or 'undefined'.
1067
Ted Kremenek356e9d62009-07-22 04:35:42 +00001068#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001069 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001070#else
1071 if (R->hasStackStorage()) {
1072#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001073 // All stack variables are considered to have undefined values
1074 // upon creation. All heap allocated blocks are considered to
1075 // have undefined values as well unless they are explicitly bound
1076 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001077 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001078 }
1079
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001080 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001081 return SValuator::CastResult(state,
1082 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001083}
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001085std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001086RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001087 if (Optional<SVal> OV = getDirectBinding(B, R))
1088 if (const nonloc::LazyCompoundVal *V =
1089 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1090 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001092 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1093 const std::pair<const GRState *, const MemRegion *> &X =
1094 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001096 if (X.first)
1097 return std::make_pair(X.first,
1098 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001099 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001100 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1101 const std::pair<const GRState *, const MemRegion *> &X =
1102 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001104 if (X.first)
1105 return std::make_pair(X.first,
1106 MRMgr.getFieldRegionWithSuper(FR, X.second));
1107 }
1108
1109 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1110}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001111
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001112SVal RegionStoreManager::RetrieveElement(const GRState* state,
1113 const ElementRegion* R) {
1114 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001115 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001116 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001117 return *V;
1118
Ted Kremenek921109a2009-07-01 23:19:52 +00001119 const MemRegion* superR = R->getSuperRegion();
1120
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001121 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001122 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001123 // FIXME: Handle loads from strings where the literal is treated as
1124 // an integer, e.g., *((unsigned int*)"hello")
1125 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001126 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001127 if (T != Ctx.getCanonicalType(R->getElementType()))
1128 return UnknownVal();
1129
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001130 const StringLiteral *Str = StrR->getStringLiteral();
1131 SVal Idx = R->getIndex();
1132 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1133 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001134 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001135 if (i > byteLength) {
1136 // Buffer overflow checking in GRExprEngine should handle this case,
1137 // but we shouldn't rely on it to not overflow here if that checking
1138 // is disabled.
1139 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001140 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001141 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001142 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001143 }
1144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001146 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001147 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001148 if (SymbolRef parentSym = V->getAsSymbol())
1149 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001150
1151 if (V->isUnknownOrUndef())
1152 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001153
1154 // Handle LazyCompoundVals for the immediate super region. Other cases
1155 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001156 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001157 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001159 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1160 return RetrieveElement(LCV->getState(), R);
1161 }
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Ted Kremeneka6275a52009-07-15 02:31:43 +00001163 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001164 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001165 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001166
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001167 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001168}
1169
Mike Stump1eb44332009-09-09 15:08:12 +00001170SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001171 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001172
1173 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001174 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001175 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001176 return *V;
1177
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001178 QualType Ty = R->getValueType(getContext());
1179 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1180}
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001182SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1183 const TypedRegion *R,
1184 QualType Ty,
1185 const MemRegion *superR) {
1186
Mike Stump1eb44332009-09-09 15:08:12 +00001187 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001188 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001190 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001192 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001193 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001194 if (SymbolRef parentSym = D->getAsSymbol())
1195 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001196
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001197 if (D->isZeroConstant())
1198 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001200 if (D->isUnknown())
1201 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001203 assert(0 && "Unknown default value");
1204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001206 // If our super region is a field or element itself, walk up the region
1207 // hierarchy to see if there is a default value installed in an ancestor.
1208 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1209 superR = cast<SubRegion>(superR)->getSuperRegion();
1210 continue;
1211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001213 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001216 // Lazy binding?
1217 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001218 const MemRegion *lazyBindingRegion = NULL;
1219 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001221 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001222 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001224 if (isa<ElementRegion>(R))
1225 return RetrieveElement(lazyBindingState,
1226 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001228 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001229 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001230 }
1231
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001232 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001234 if (isa<ElementRegion>(R)) {
1235 // Currently we don't reason specially about Clang-style vectors. Check
1236 // if superR is a vector and if so return Unknown.
1237 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1238 if (typedSuperR->getValueType(getContext())->isVectorType())
1239 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001240 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001241 }
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001243 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001246 // All other values are symbolic.
1247 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001248}
Mike Stump1eb44332009-09-09 15:08:12 +00001249
1250SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001251 const ObjCIvarRegion* R) {
1252
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001253 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001254 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001255
Zhongxing Xu13d50172009-10-11 08:08:02 +00001256 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001257 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001259 const MemRegion *superR = R->getSuperRegion();
1260
Ted Kremenekab22ee92009-10-20 01:20:57 +00001261 // Check if the super region has a default binding.
1262 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001263 if (SymbolRef parentSym = V->getAsSymbol())
1264 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001266 // Other cases: give up.
1267 return UnknownVal();
1268 }
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Ted Kremenek25c54572009-07-20 22:58:02 +00001270 return RetrieveLazySymbol(state, R);
1271}
1272
Ted Kremenek9031dd72009-07-21 00:12:07 +00001273SVal RegionStoreManager::RetrieveVar(const GRState *state,
1274 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenek9031dd72009-07-21 00:12:07 +00001276 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001277 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Zhongxing Xu13d50172009-10-11 08:08:02 +00001279 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001280 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenek9031dd72009-07-21 00:12:07 +00001282 // Lazily derive a value for the VarRegion.
1283 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Ted Kremenek9031dd72009-07-21 00:12:07 +00001285 if (R->hasGlobalsOrParametersStorage())
1286 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Ted Kremenek9031dd72009-07-21 00:12:07 +00001288 return UndefinedVal();
1289}
1290
Mike Stump1eb44332009-09-09 15:08:12 +00001291SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001292 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Ted Kremenek25c54572009-07-20 22:58:02 +00001294 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001295
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001296 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001297 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001298}
1299
Mike Stump1eb44332009-09-09 15:08:12 +00001300SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1301 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001302 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001303 assert(T->isStructureType());
1304
Zhongxing Xub7507d12009-06-11 07:27:30 +00001305 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001306 RecordDecl* RD = RT->getDecl();
1307 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001308 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001309#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001310 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1311
Ted Kremenek67f28532009-06-17 22:02:04 +00001312 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1313 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001314 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001315
Douglas Gregore267ff32008-12-11 20:41:00 +00001316 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1317 FieldEnd = Fields.rend();
1318 Field != FieldEnd; ++Field) {
1319 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001320 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001321 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001322 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1323 }
1324
Zhongxing Xud91ee272009-06-23 09:02:15 +00001325 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001326#else
1327 return ValMgr.makeLazyCompoundVal(state, R);
1328#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001329}
1330
Ted Kremenek67f28532009-06-17 22:02:04 +00001331SVal RegionStoreManager::RetrieveArray(const GRState *state,
1332 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001333#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001334 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001335 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1336
1337 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001338 uint64_t size = CAT->getSize().getZExtValue();
1339 for (uint64_t i = 0; i < size; ++i) {
1340 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001341 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001342 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001343 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001344 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001345 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1346 }
1347
Zhongxing Xud91ee272009-06-23 09:02:15 +00001348 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001349#else
1350 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1351 return ValMgr.makeLazyCompoundVal(state, R);
1352#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001353}
1354
Ted Kremenek9af46f52009-06-16 22:36:44 +00001355//===----------------------------------------------------------------------===//
1356// Binding values to regions.
1357//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001358
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001359Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001360 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Ted Kremenek0964a062009-01-21 06:57:53 +00001362 if (isa<loc::MemRegionVal>(L))
1363 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001364
Ted Kremenek0964a062009-01-21 06:57:53 +00001365 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001366 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001367 return RBFactory.Remove(B, R).getRoot();
1368 }
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Ted Kremenek0964a062009-01-21 06:57:53 +00001370 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001371}
1372
Ted Kremenek67f28532009-06-17 22:02:04 +00001373const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001374 if (isa<loc::ConcreteInt>(L))
1375 return state;
1376
Ted Kremenek9af46f52009-06-16 22:36:44 +00001377 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001378 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Ted Kremenek9af46f52009-06-16 22:36:44 +00001380 // Check if the region is a struct region.
1381 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1382 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001383 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001385 // Special case: the current region represents a cast and it and the super
1386 // region both have pointer types or intptr_t types. If so, perform the
1387 // bind to the super region.
1388 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001389 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001390 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1391 if (ER->getIndex().isZeroConstant()) {
1392 if (const TypedRegion *superR =
1393 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1394 ASTContext &Ctx = getContext();
1395 QualType superTy = superR->getValueType(Ctx);
1396 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001397
1398 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001399 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001400 SValuator::CastResult cr =
1401 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001402 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1403 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001404 // For now, just invalidate the fields of the struct/union/class.
1405 // FIXME: Precisely handle the fields of the record.
1406 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001407 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001408 }
1409 }
1410 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001411 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1412 // Binding directly to a symbolic region should be treated as binding
1413 // to element 0.
1414 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001415 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001416 R = GetElementZeroRegion(SR, T);
1417 }
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001419 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001420 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001421 return state->makeWithStore(
1422 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001423}
1424
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001425const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001426 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001427 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001428
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001429 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001430
Ted Kremenek0964a062009-01-21 06:57:53 +00001431 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001432 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001433 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001434 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001435
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001436 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001437}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001438
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001439// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001440const GRState *
1441RegionStoreManager::BindCompoundLiteral(const GRState *state,
1442 const CompoundLiteralExpr* CL,
1443 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001445 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001446 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001447}
1448
Ted Kremenek027e2662009-11-19 20:20:24 +00001449const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1450 const MemRegion *R,
1451 QualType T) {
1452 Store store = state->getStore();
1453 RegionBindings B = GetRegionBindings(store);
1454 SVal V;
1455
1456 if (Loc::IsLocType(T))
1457 V = ValMgr.makeNull();
1458 else if (T->isIntegerType())
1459 V = ValMgr.makeZeroVal(T);
1460 else if (T->isStructureType() || T->isArrayType()) {
1461 // Set the default value to a zero constant when it is a structure
1462 // or array. The type doesn't really matter.
1463 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1464 }
1465 else {
1466 return state;
1467 }
1468
1469 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1470 return state->makeWithStore(B.getRoot());
1471}
1472
Ted Kremenek67f28532009-06-17 22:02:04 +00001473const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001474 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001475 SVal Init) {
1476
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001477 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001478 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001479 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001480
Ted Kremenek46537392009-07-16 01:33:37 +00001481 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001482
1483 // Check if the init expr is a StringLiteral.
1484 if (isa<loc::MemRegionVal>(Init)) {
1485 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1486 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1487 const char* str = S->getStrData();
1488 unsigned len = S->getByteLength();
1489 unsigned j = 0;
1490
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001491 // Copy bytes from the string literal into the target array. Trailing bytes
1492 // in the array that are not covered by the string literal are initialized
1493 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001494 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001495 if (j >= len)
1496 break;
1497
Ted Kremenek46537392009-07-16 01:33:37 +00001498 SVal Idx = ValMgr.makeArrayIndex(i);
1499 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1500 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001501
Zhongxing Xud91ee272009-06-23 09:02:15 +00001502 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001503 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001504 }
1505
Ted Kremenek67f28532009-06-17 22:02:04 +00001506 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001507 }
1508
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001509 // Handle lazy compound values.
1510 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1511 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001512
1513 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001514
1515 if (Init.isUnknown())
1516 return setImplicitDefaultValue(state, R, ElementTy);
1517
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001518 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001519 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001520 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Ted Kremenek46537392009-07-16 01:33:37 +00001522 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001523 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001524 if (VI == VE)
1525 break;
1526
Ted Kremenek46537392009-07-16 01:33:37 +00001527 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001528 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001529
1530 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001531 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001532 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001533 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001534 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001535 }
1536
Ted Kremenek027e2662009-11-19 20:20:24 +00001537 // If the init list is shorter than the array length, set the
1538 // array default value.
1539 if (i < size)
1540 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001541
Ted Kremenek67f28532009-06-17 22:02:04 +00001542 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001543}
1544
Ted Kremenek67f28532009-06-17 22:02:04 +00001545const GRState *
1546RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1547 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Ted Kremenek67f28532009-06-17 22:02:04 +00001549 if (!Features.supportsFields())
1550 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001552 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001553 assert(T->isStructureType());
1554
Ted Kremenek6217b802009-07-29 21:53:49 +00001555 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001556 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001557
1558 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001559 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001560
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001561 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001562 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001563 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek67f28532009-06-17 22:02:04 +00001565 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1566 // Ignore them and kill the field values.
1567 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001568 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001569
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001570 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001571 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001572
1573 RecordDecl::field_iterator FI, FE;
1574
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001575 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001576
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001577 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001578 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001579
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001580 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001581 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001582
Ted Kremenekcf549592009-09-22 21:19:14 +00001583 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001584 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001585 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001586 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001587 else
1588 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001589 }
1590
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001591 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001592 if (FI != FE) {
1593 Store store = state->getStore();
1594 RegionBindings B = GetRegionBindings(store);
1595 B = RBFactory.Add(B, R,
1596 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1597 state = state->makeWithStore(B.getRoot());
1598 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001599
Ted Kremenek67f28532009-06-17 22:02:04 +00001600 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001601}
1602
Zhongxing Xu13d50172009-10-11 08:08:02 +00001603Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1604 RegionBindings B = GetRegionBindings(store);
1605 llvm::OwningPtr<RegionStoreSubRegionMap>
1606 SubRegions(getRegionStoreSubRegionMap(store));
1607 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001608
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001609 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001610 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001611
Zhongxing Xu13d50172009-10-11 08:08:02 +00001612 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001613}
1614
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001615const GRState*
1616RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1617 const GRState *state,
1618 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001619
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001620 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001621 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001622
Mike Stump1eb44332009-09-09 15:08:12 +00001623 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001624 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001625
Mike Stump1eb44332009-09-09 15:08:12 +00001626 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001627 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001629 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1630 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001631 return state->makeWithStore(
1632 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001633}
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Ted Kremenek9af46f52009-06-16 22:36:44 +00001635//===----------------------------------------------------------------------===//
1636// State pruning.
1637//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001638
Mike Stump1eb44332009-09-09 15:08:12 +00001639void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001640 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001641 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001642{
Ted Kremenek781115c2009-10-17 17:45:11 +00001643 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1644
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001645 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001646 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Ted Kremenek9af46f52009-06-16 22:36:44 +00001648 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001649 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001650 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001651
1652 // Do a pass over the regions in the store. For VarRegions we check if
1653 // the variable is still live and if so add it to the list of live roots.
1654 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001655 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001656
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001657 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001658 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001659 const MemRegion *R = I.getKey();
1660 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001661 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001662
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001663 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001664 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001665 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001666 llvm::SmallVector<RBDNode, 10> Postponed;
1667
Zhongxing Xu6800b332009-10-18 04:15:47 +00001668 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001669
Ted Kremenek9af46f52009-06-16 22:36:44 +00001670 while (!IntermediateRoots.empty()) {
1671 const MemRegion* R = IntermediateRoots.back();
1672 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001673
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001674 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001675 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001676 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001677
Ted Kremenek9af46f52009-06-16 22:36:44 +00001678 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001679 if (SymReaper.isLive(Loc, VR->getDecl()))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001680 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001681 continue;
1682 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001683
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001684 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001685 llvm::SmallVectorImpl<RBDNode> &Q =
1686 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1687
1688 Q.push_back(std::make_pair(&state, SR));
1689
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001690 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001691 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001692
1693 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001694 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001695 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001696 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001699 // Enqueue the RegionRoots onto WorkList.
1700 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1701 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001702 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001703 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001704 RegionRoots.clear();
1705
Zhongxing Xu6800b332009-10-18 04:15:47 +00001706 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001707
Ted Kremenek01756192009-10-29 05:14:17 +00001708tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001709 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001710 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001711 WorkList.pop_back();
1712
1713 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001714 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001715 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001716 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001718 const MemRegion *R = N.second;
1719 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001720
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001721 // Enqueue subregions.
1722 RegionStoreSubRegionMap *M;
1723
1724 if (&state == state_N)
1725 M = SubRegions.get();
1726 else {
1727 RegionStoreSubRegionMap *& SM = SC[state_N];
1728 if (!SM)
1729 SM = getRegionStoreSubRegionMap(state_N->getStore());
1730 M = SM;
1731 }
1732
1733 RegionStoreSubRegionMap::iterator I, E;
1734 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1735 WorkList.push_back(std::make_pair(state_N, *I));
1736
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001737 // Enqueue the super region.
1738 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1739 const MemRegion *superR = SR->getSuperRegion();
1740 if (!isa<MemSpaceRegion>(superR)) {
1741 // If 'R' is a field or an element, we want to keep the bindings
1742 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001743 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001744 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1745 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001746 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001747 }
1748 }
1749
1750 // Mark the symbol for any live SymbolicRegion as "live". This means we
1751 // should continue to track that symbol.
1752 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1753 SymReaper.markLive(SymR->getSymbol());
1754
1755 Store store_N = state_N->getStore();
1756 RegionBindings B_N = GetRegionBindings(store_N);
1757
1758 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001759 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001760
Zhongxing Xu13d50172009-10-11 08:08:02 +00001761 if (V) {
1762 // Check for lazy bindings.
1763 if (const nonloc::LazyCompoundVal *LCV =
1764 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001765
Zhongxing Xu13d50172009-10-11 08:08:02 +00001766 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001767 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001768 }
1769 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001770 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001771 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001772 SI!=SE;++SI)
1773 SymReaper.markLive(*SI);
1774
Zhongxing Xu13d50172009-10-11 08:08:02 +00001775 // If V is a region, then add it to the worklist.
1776 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001777 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001778 }
1779 }
1780 }
1781
Ted Kremenek01756192009-10-29 05:14:17 +00001782 // See if any postponed SymbolicRegions are actually live now, after
1783 // having done a scan.
1784 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1785 E = Postponed.end() ; I != E ; ++I) {
1786 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1787 if (SymReaper.isLive(SR->getSymbol())) {
1788 WorkList.push_back(*I);
1789 I->second = NULL;
1790 }
1791 }
1792 }
1793
1794 if (!WorkList.empty())
1795 goto tryAgain;
1796
Ted Kremenek9af46f52009-06-16 22:36:44 +00001797 // We have now scanned the store, marking reachable regions and symbols
1798 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001799 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001800 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001801 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001802 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001803 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Ted Kremenek9af46f52009-06-16 22:36:44 +00001806 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001807 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Ted Kremenek9af46f52009-06-16 22:36:44 +00001809 // Mark all non-live symbols that this region references as dead.
1810 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1811 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Zhongxing Xu13d50172009-10-11 08:08:02 +00001813 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001814 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1815 for (; SI != SE; ++SI)
1816 SymReaper.maybeDead(*SI);
1817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001819 // Write the store back.
1820 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001821}
1822
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001823GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1824 StackFrameContext const *frame) {
1825 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1826 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1827
1828 FunctionDecl::param_const_iterator PI = FD->param_begin();
1829
1830 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1831
1832 // Copy the arg expression value to the arg variables.
1833 for (; AI != AE; ++AI, ++PI) {
1834 SVal ArgVal = state->getSVal(*AI);
1835 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1836 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1837 }
1838
1839 return state;
1840}
1841
Ted Kremenek9af46f52009-06-16 22:36:44 +00001842//===----------------------------------------------------------------------===//
1843// Utility methods.
1844//===----------------------------------------------------------------------===//
1845
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001846void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001847 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001848 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001849 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Ted Kremenek451ac092009-08-06 04:50:20 +00001851 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001852 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001853}