blob: 63bfa97c4d13ea5c95a27d07cd29ed503998c4ec [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 {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167 Map::iterator I = M.find(Parent);
168
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);
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000219 /// getLValueString - Returns an SVal representing the lvalue of a
220 /// StringLiteral. Within RegionStore a StringLiteral has an
221 /// associated StringRegion, and the lvalue of a StringLiteral is
222 /// the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000223 SVal getLValueString(const GRState *state, const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000224
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000225 /// getLValueCompoundLiteral - Returns an SVal representing the
226 /// lvalue of a compound literal. Within RegionStore a compound
227 /// literal has an associated region, and the lvalue of the
228 /// compound literal is the lvalue of that region.
Ted Kremenek67f28532009-06-17 22:02:04 +0000229 SVal getLValueCompoundLiteral(const GRState *state, const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000230
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000231 /// getLValueVar - Returns an SVal that represents the lvalue of a
232 /// variable. Within RegionStore a variable has an associated
233 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000234 SVal getLValueVar(const GRState *ST, const VarDecl *VD,
235 const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Ted Kremenek67f28532009-06-17 22:02:04 +0000237 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000238
Ted Kremenek67f28532009-06-17 22:02:04 +0000239 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Ted Kremenek67f28532009-06-17 22:02:04 +0000241 SVal getLValueFieldOrIvar(const GRState *state, SVal Base, const Decl* D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000242
Ted Kremenek67f28532009-06-17 22:02:04 +0000243 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +0000244 SVal Base, SVal Offset);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000245
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000246
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000247 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
248 /// type. 'Array' represents the lvalue of the array being decayed
249 /// to a pointer, and the returned SVal represents the decayed
250 /// version of that lvalue (i.e., a pointer to the first element of
251 /// the array). This is called by GRExprEngine when evaluating
252 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000253 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000254
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000255 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000256 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000257
Mike Stump1eb44332009-09-09 15:08:12 +0000258 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000259 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000260 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000261
Ted Kremenek67f28532009-06-17 22:02:04 +0000262 //===-------------------------------------------------------------------===//
263 // Binding values to regions.
264 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000265
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000266 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
267 const Expr *E, unsigned Count);
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000269private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000270 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000271 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
273public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000274 const GRState *Bind(const GRState *state, Loc LV, SVal V);
275
276 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000277 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000279 const GRState *BindDecl(const GRState *ST, const VarDecl *VD,
280 const LocationContext *LC, SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000281
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000282 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl*,
283 const LocationContext *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000284 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000285 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000286
Ted Kremenek67f28532009-06-17 22:02:04 +0000287 /// BindStruct - Bind a compound value to a structure.
288 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremenek67f28532009-06-17 22:02:04 +0000290 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
292 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000293 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000294
Ted Kremenek67f28532009-06-17 22:02:04 +0000295 Store Remove(Store store, Loc LV);
296
297 //===------------------------------------------------------------------===//
298 // Loading values from regions.
299 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenek67f28532009-06-17 22:02:04 +0000301 /// The high level logic for this method is this:
302 /// Retrieve (L)
303 /// if L has binding
304 /// return L's binding
305 /// else if L is in killset
306 /// return unknown
307 /// else
308 /// if L is on stack or heap
309 /// return undefined
310 /// else
311 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000312 SValuator::CastResult Retrieve(const GRState *state, Loc L,
313 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000314
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000315 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000316
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000317 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000319 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Ted Kremenek9031dd72009-07-21 00:12:07 +0000321 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Ted Kremenek25c54572009-07-20 22:58:02 +0000323 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000325 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
326 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek67f28532009-06-17 22:02:04 +0000328 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000329 /// struct copy:
330 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000331 /// x = y;
332 /// y's value is retrieved by this method.
333 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek67f28532009-06-17 22:02:04 +0000335 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000337 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000338 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000340 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
341 const GRState *state,
342 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000343
Ted Kremenek0954cde2009-09-24 04:11:44 +0000344 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
345 QualType T);
346
Ted Kremenek67f28532009-06-17 22:02:04 +0000347 //===------------------------------------------------------------------===//
348 // State pruning.
349 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Ted Kremenek67f28532009-06-17 22:02:04 +0000351 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
352 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000353 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000354 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
355
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000356 const GRState *EnterStackFrame(const GRState *state,
357 const StackFrameContext *frame);
358
Ted Kremenek67f28532009-06-17 22:02:04 +0000359 //===------------------------------------------------------------------===//
360 // Region "extents".
361 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Ted Kremenek67f28532009-06-17 22:02:04 +0000363 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
364 SVal getSizeInElements(const GRState *state, const MemRegion* R);
365
366 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000367 // Utility methods.
368 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000369
Ted Kremenek451ac092009-08-06 04:50:20 +0000370 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000371 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000372 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000373
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000374 void print(Store store, llvm::raw_ostream& Out, const char* nl,
375 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000376
377 void iterBindings(Store store, BindingsHandler& f) {
378 // FIXME: Implement.
379 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000380
Ted Kremenek67f28532009-06-17 22:02:04 +0000381 // FIXME: Remove.
382 BasicValueFactory& getBasicVals() {
383 return StateMgr.getBasicVals();
384 }
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek67f28532009-06-17 22:02:04 +0000386 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000387 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000388};
389
390} // end anonymous namespace
391
Ted Kremenek9af46f52009-06-16 22:36:44 +0000392//===----------------------------------------------------------------------===//
393// RegionStore creation.
394//===----------------------------------------------------------------------===//
395
396StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
397 RegionStoreFeatures F = maximal_features_tag();
398 return new RegionStoreManager(StMgr, F);
399}
400
401StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
402 RegionStoreFeatures F = minimal_features_tag();
403 F.enableFields(true);
404 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000405}
406
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000407void
408RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000409 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000410 const MemRegion *superR = R->getSuperRegion();
411 if (add(superR, R))
412 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000413 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414}
415
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000416RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000417RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
418 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000419 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000421 llvm::SmallVector<const SubRegion*, 10> WL;
422
Ted Kremenek451ac092009-08-06 04:50:20 +0000423 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000424 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
425 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Mike Stump1eb44332009-09-09 15:08:12 +0000427 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428 // don't have direct bindings but are super regions of those that do.
429 while (!WL.empty()) {
430 const SubRegion *R = WL.back();
431 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000432 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000433 }
434
Ted Kremenek14453bf2009-03-03 19:02:42 +0000435 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000436}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000437
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000438SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000439 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000440}
441
Ted Kremenek9af46f52009-06-16 22:36:44 +0000442//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000443// Binding invalidation.
444//===----------------------------------------------------------------------===//
445
Zhongxing Xu13d50172009-10-11 08:08:02 +0000446void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
447 const MemRegion *R,
448 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000449 RegionStoreSubRegionMap::iterator I, E;
450
451 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000452 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000454 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000455}
456
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000457const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
458 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000459 const Expr *Ex,
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000460 unsigned Count) {
461 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000463 // Strip away casts.
464 R = R->getBaseRegion();
465
Ted Kremenek87806792009-09-27 20:45:21 +0000466 // Get the mapping of regions -> subregions.
467 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000468 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000469
470 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000471
Ted Kremenek87806792009-09-27 20:45:21 +0000472 llvm::DenseMap<const MemRegion *, unsigned> Visited;
473 llvm::SmallVector<const MemRegion *, 10> WorkList;
474 WorkList.push_back(R);
475
476 while (!WorkList.empty()) {
477 R = WorkList.back();
478 WorkList.pop_back();
479
480 // Have we visited this region before?
481 unsigned &visited = Visited[R];
482 if (visited)
483 continue;
484 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Ted Kremenek87806792009-09-27 20:45:21 +0000486 // Add subregions to work list.
487 RegionStoreSubRegionMap::iterator I, E;
488 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
489 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000490
491 // Get the old binding. Is it a region? If so, add it to the worklist.
492 if (Optional<SVal> V = getDirectBinding(B, R)) {
493 if (const MemRegion *RV = V->getAsRegion())
494 WorkList.push_back(RV);
495 }
496
Ted Kremenek87806792009-09-27 20:45:21 +0000497 // Handle region.
498 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
499 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000500 // Invalidate the region by setting its default value to
501 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000502 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
503 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000504 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000505 continue;
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Ted Kremenek87806792009-09-27 20:45:21 +0000508 if (!R->isBoundable())
509 continue;
510
511 const TypedRegion *TR = cast<TypedRegion>(R);
512 QualType T = TR->getValueType(Ctx);
513
514 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000515 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
516
Zhongxing Xu13d50172009-10-11 08:08:02 +0000517 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000518 if (!RD)
519 continue;
520
Zhongxing Xu13d50172009-10-11 08:08:02 +0000521 // Invalidate the region by setting its default value to
522 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000523 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
524 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000525 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000526 continue;
527 }
528
529 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
530 // Set the default value of the array to conjured symbol.
531 DefinedOrUnknownSVal V =
532 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000533 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000534 continue;
535 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000536
537 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
538 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000539 // For fields and elements whose super region has also been invalidated,
540 // only remove the old binding. The super region will get set with a
541 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000542 B = RBFactory.Remove(B, R);
543 continue;
544 }
Ted Kremenek87806792009-09-27 20:45:21 +0000545
Ted Kremenek389c44c2009-09-29 03:12:50 +0000546 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000547 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
548 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000549 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000550 }
551
Ted Kremenek87806792009-09-27 20:45:21 +0000552 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000553 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000554}
555
556//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000557// getLValueXXX methods.
558//===----------------------------------------------------------------------===//
559
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000560/// getLValueString - Returns an SVal representing the lvalue of a
561/// StringLiteral. Within RegionStore a StringLiteral has an
562/// associated StringRegion, and the lvalue of a StringLiteral is the
563/// lvalue of that region.
Mike Stump1eb44332009-09-09 15:08:12 +0000564SVal RegionStoreManager::getLValueString(const GRState *St,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000565 const StringLiteral* S) {
566 return loc::MemRegionVal(MRMgr.getStringRegion(S));
567}
568
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000569/// getLValueVar - Returns an SVal that represents the lvalue of a
570/// variable. Within RegionStore a variable has an associated
571/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000572SVal RegionStoreManager::getLValueVar(const GRState *ST, const VarDecl *VD,
573 const LocationContext *LC) {
574 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000575}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000576
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000577/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
578/// of a compound literal. Within RegionStore a compound literal
579/// has an associated region, and the lvalue of the compound literal
580/// is the lvalue of that region.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000581SVal RegionStoreManager::getLValueCompoundLiteral(const GRState *St,
582 const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000583 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
584}
585
Ted Kremenek67f28532009-06-17 22:02:04 +0000586SVal RegionStoreManager::getLValueIvar(const GRState *St, const ObjCIvarDecl* D,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000587 SVal Base) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000588 return getLValueFieldOrIvar(St, Base, D);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000589}
590
Ted Kremenek67f28532009-06-17 22:02:04 +0000591SVal RegionStoreManager::getLValueField(const GRState *St, SVal Base,
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000592 const FieldDecl* D) {
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000593 return getLValueFieldOrIvar(St, Base, D);
594}
595
Ted Kremenek67f28532009-06-17 22:02:04 +0000596SVal RegionStoreManager::getLValueFieldOrIvar(const GRState *St, SVal Base,
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000597 const Decl* D) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000598 if (Base.isUnknownOrUndef())
599 return Base;
600
601 Loc BaseL = cast<Loc>(Base);
602 const MemRegion* BaseR = 0;
603
604 switch (BaseL.getSubKind()) {
605 case loc::MemRegionKind:
606 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
607 break;
608
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000609 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000610 // These are anormal cases. Flag an undefined value.
611 return UndefinedVal();
612
613 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000614 // While these seem funny, this can happen through casts.
615 // FIXME: What we should return is the field offset. For example,
616 // add the field offset to the integer value. That way funny things
617 // like this work properly: &(((struct foo *) 0xa)->f)
618 return Base;
619
620 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000621 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000622 return Base;
623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000625 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
626 // of FieldDecl.
627 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
628 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000629
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000630 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000631}
632
Ted Kremenek67f28532009-06-17 22:02:04 +0000633SVal RegionStoreManager::getLValueElement(const GRState *St,
Ted Kremenekf936f452009-05-04 06:18:28 +0000634 QualType elementType,
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000635 SVal Base, SVal Offset) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000636
Ted Kremenekde7ec632009-03-09 22:44:49 +0000637 // If the base is an unknown or undefined value, just return it back.
638 // FIXME: For absolute pointer addresses, we just return that value back as
639 // well, although in reality we should return the offset added to that
640 // value.
641 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000642 return Base;
643
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000644 // Only handle integer offsets... for now.
645 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000646 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000647
Zhongxing Xuce760782009-05-09 13:20:07 +0000648 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000649
650 // Pointer of any type can be cast and used as array base.
651 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Ted Kremenek46537392009-07-16 01:33:37 +0000653 // Convert the offset to the appropriate size and signedness.
654 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000656 if (!ElemR) {
657 //
658 // If the base region is not an ElementRegion, create one.
659 // This can happen in the following example:
660 //
661 // char *p = __builtin_alloc(10);
662 // p[1] = 8;
663 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000664 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000665 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000666 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000667 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000668 }
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000670 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000672 if (!isa<nonloc::ConcreteInt>(BaseIdx))
673 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000675 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
676 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
677 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Ted Kremenek46537392009-07-16 01:33:37 +0000679 // Compute the new index.
680 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Ted Kremenek46537392009-07-16 01:33:37 +0000682 // Construct the new ElementRegion.
683 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000684 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000685 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000686}
687
Ted Kremenek9af46f52009-06-16 22:36:44 +0000688//===----------------------------------------------------------------------===//
689// Extents for regions.
690//===----------------------------------------------------------------------===//
691
Ted Kremenek67f28532009-06-17 22:02:04 +0000692SVal RegionStoreManager::getSizeInElements(const GRState *state,
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000693 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000695 switch (R->getKind()) {
696 case MemRegion::MemSpaceRegionKind:
697 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000698 return UnknownVal();
699
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000700 case MemRegion::CodeTextRegionKind:
701 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000702 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000703
704 // Not yet handled.
705 case MemRegion::AllocaRegionKind:
706 case MemRegion::CompoundLiteralRegionKind:
707 case MemRegion::ElementRegionKind:
708 case MemRegion::FieldRegionKind:
709 case MemRegion::ObjCIvarRegionKind:
710 case MemRegion::ObjCObjectRegionKind:
711 case MemRegion::SymbolicRegionKind:
712 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000714 case MemRegion::StringRegionKind: {
715 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000716 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717 // operations with signed indices.
718 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000721 case MemRegion::VarRegionKind: {
722 const VarRegion* VR = cast<VarRegion>(R);
723 // Get the type of the variable.
724 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000726 // FIXME: Handle variable-length arrays.
727 if (isa<VariableArrayType>(T))
728 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000730 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
731 // return the size as signed integer.
732 return ValMgr.makeIntVal(CAT->getSize(), false);
733 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000734
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000735 // Clients can use ordinary variables as if they were arrays. These
736 // essentially are arrays of size 1.
737 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000740 case MemRegion::BEG_DECL_REGIONS:
741 case MemRegion::END_DECL_REGIONS:
742 case MemRegion::BEG_TYPED_REGIONS:
743 case MemRegion::END_TYPED_REGIONS:
744 assert(0 && "Infeasible region");
745 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000748 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000749 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000750}
751
Ted Kremenek67f28532009-06-17 22:02:04 +0000752const GRState *RegionStoreManager::setExtent(const GRState *state,
753 const MemRegion *region,
754 SVal extent) {
755 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000756}
757
758//===----------------------------------------------------------------------===//
759// Location and region casting.
760//===----------------------------------------------------------------------===//
761
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000762/// ArrayToPointer - Emulates the "decay" of an array to a pointer
763/// type. 'Array' represents the lvalue of the array being decayed
764/// to a pointer, and the returned SVal represents the decayed
765/// version of that lvalue (i.e., a pointer to the first element of
766/// the array). This is called by GRExprEngine when evaluating casts
767/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000768SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000769 if (!isa<loc::MemRegionVal>(Array))
770 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Ted Kremenekabb042f2008-12-13 19:24:37 +0000772 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
773 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000775 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000776 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000778 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000779 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000780 ArrayType *AT = cast<ArrayType>(T);
781 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek75185b52009-07-16 00:00:11 +0000783 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
784 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000785
786 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000787}
788
Ted Kremenek9af46f52009-06-16 22:36:44 +0000789//===----------------------------------------------------------------------===//
790// Pointer arithmetic.
791//===----------------------------------------------------------------------===//
792
Mike Stump1eb44332009-09-09 15:08:12 +0000793SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000794 BinaryOperator::Opcode Op, Loc L, NonLoc R,
795 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000796 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000797 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000798 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000799
Zhongxing Xua1718c72009-04-03 07:33:13 +0000800 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000801 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000802
Ted Kremenek3bccf082009-07-11 00:58:27 +0000803 switch (MR->getKind()) {
804 case MemRegion::SymbolicRegionKind: {
805 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000806 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000807 QualType T = Sym->getType(getContext());
808 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000810 if (const PointerType *PT = T->getAs<PointerType>())
811 EleTy = PT->getPointeeType();
812 else
John McCall183700f2009-09-21 23:43:11 +0000813 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Ted Kremenek3bccf082009-07-11 00:58:27 +0000815 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
816 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000817 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000818 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000819 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000820 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000821 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000822 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000823 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
824 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000825 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000826 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000827
Ted Kremenek3bccf082009-07-11 00:58:27 +0000828 case MemRegion::ElementRegionKind: {
829 ER = cast<ElementRegion>(MR);
830 break;
831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Ted Kremenek3bccf082009-07-11 00:58:27 +0000833 // Not yet handled.
834 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000835 case MemRegion::StringRegionKind: {
836
837 }
838 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000839 case MemRegion::CompoundLiteralRegionKind:
840 case MemRegion::FieldRegionKind:
841 case MemRegion::ObjCObjectRegionKind:
842 case MemRegion::ObjCIvarRegionKind:
843 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 case MemRegion::CodeTextRegionKind:
846 // Technically this can happen if people do funny things with casts.
847 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 case MemRegion::MemSpaceRegionKind:
850 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
851 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek3bccf082009-07-11 00:58:27 +0000853 case MemRegion::BEG_DECL_REGIONS:
854 case MemRegion::END_DECL_REGIONS:
855 case MemRegion::BEG_TYPED_REGIONS:
856 case MemRegion::END_TYPED_REGIONS:
857 assert(0 && "Infeasible region");
858 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000859 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000860
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000861 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000862 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000863
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000864 // For now, only support:
865 // (a) concrete integer indices that can easily be resolved
866 // (b) 0 + symbolic index
867 if (Base) {
868 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
869 // FIXME: Should use SValuator here.
870 SVal NewIdx =
871 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000872 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000873 const MemRegion* NewER =
874 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
875 ER->getSuperRegion(), getContext());
876 return ValMgr.makeLoc(NewER);
877 }
878 if (0 == Base->getValue()) {
879 const MemRegion* NewER =
880 MRMgr.getElementRegion(ER->getElementType(), R,
881 ER->getSuperRegion(), getContext());
882 return ValMgr.makeLoc(NewER);
883 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Ted Kremenek5dc27462009-03-03 02:51:43 +0000886 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000887}
888
Ted Kremenek9af46f52009-06-16 22:36:44 +0000889//===----------------------------------------------------------------------===//
890// Loading values from regions.
891//===----------------------------------------------------------------------===//
892
Zhongxing Xu13d50172009-10-11 08:08:02 +0000893Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
894 const MemRegion *R) {
895 if (const BindingVal *BV = B.lookup(R))
896 return Optional<SVal>::create(BV->getDirectValue());
897
898 return Optional<SVal>();
899}
900
901Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000902 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000904 if (R->isBoundable())
905 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
906 if (TR->getValueType(getContext())->isUnionType())
907 return UnknownVal();
908
Zhongxing Xu13d50172009-10-11 08:08:02 +0000909 if (BindingVal const *V = B.lookup(R))
910 return Optional<SVal>::create(V->getDefaultValue());
911
912 return Optional<SVal>();
913}
914
915Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
916 const MemRegion *R) {
917 if (const BindingVal *BV = B.lookup(R))
918 return Optional<SVal>::create(BV->getValue());
919
920 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000921}
922
Ted Kremeneka6275a52009-07-15 02:31:43 +0000923static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
924 RTy = Ctx.getCanonicalType(RTy);
925 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Ted Kremeneka6275a52009-07-15 02:31:43 +0000927 if (RTy == UsedTy)
928 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000929
930
Ted Kremenek25c54572009-07-20 22:58:02 +0000931 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000932 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000933 // represents a reinterpretation.
934 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000935 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000936 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000937
938 return PUsedTy && PRTy &&
939 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000940 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000941 }
942
943 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000944}
945
Ted Kremenek0954cde2009-09-24 04:11:44 +0000946const ElementRegion *
947RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
948 ASTContext &Ctx = getContext();
949 SVal idx = ValMgr.makeZeroArrayIndex();
950 assert(!T.isNull());
951 return MRMgr.getElementRegion(T, idx, SR, Ctx);
952}
953
954
955
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000956SValuator::CastResult
957RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000958
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000959 assert(!isa<UnknownVal>(L) && "location unknown");
960 assert(!isa<UndefinedVal>(L) && "location undefined");
961
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000962 // FIXME: Is this even possible? Shouldn't this be treated as a null
963 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000964 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000965 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000966
Ted Kremenek67f28532009-06-17 22:02:04 +0000967 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000968
Zhongxing Xu91844122009-05-20 09:18:48 +0000969 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000970 // Example:
971 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000972 // char* p = alloca();
973 // read(p);
974 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000975 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000976 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Ted Kremenek0954cde2009-09-24 04:11:44 +0000978 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
979 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Ted Kremenek968f0a62009-08-03 21:41:46 +0000981 if (isa<CodeTextRegion>(MR))
982 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000984 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
985 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +0000986 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +0000987 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000988
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000989 // FIXME: We should eventually handle funny addressing. e.g.:
990 //
991 // int x = ...;
992 // int *p = &x;
993 // char *q = (char*) p;
994 // char c = *q; // returns the first byte of 'x'.
995 //
996 // Such funny addressing will occur due to layering of regions.
997
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000998#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +0000999 ASTContext &Ctx = getContext();
1000 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001001 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1002 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001003 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001004 assert(Ctx.getCanonicalType(RTy) ==
1005 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001006 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001007#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001008
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001009 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001010 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001012 // FIXME: Handle unions.
1013 if (RTy->isUnionType())
1014 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001015
1016 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001017 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001018
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001019 // FIXME: handle Vector types.
1020 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001021 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001022
1023 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001024 return CastRetrievedVal(RetrieveField(state, FR), state, FR, T);
Zhongxing Xu99c20302009-06-28 14:16:39 +00001025
1026 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001027 return CastRetrievedVal(RetrieveElement(state, ER), state, ER, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Ted Kremenek25c54572009-07-20 22:58:02 +00001029 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001030 return CastRetrievedVal(RetrieveObjCIvar(state, IVR), state, IVR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Ted Kremenek9031dd72009-07-21 00:12:07 +00001032 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001033 return CastRetrievedVal(RetrieveVar(state, VR), state, VR, T);
Ted Kremenek25c54572009-07-20 22:58:02 +00001034
Ted Kremenek451ac092009-08-06 04:50:20 +00001035 RegionBindings B = GetRegionBindings(state->getStore());
1036 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001037
1038 // Check if the region has a binding.
1039 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001040 if (SVal const *SV = V->getValue())
1041 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001042
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001043 // The location does not have a bound value. This means that it has
1044 // the value it had upon its creation and/or entry to the analyzed
1045 // function/method. These are either symbolic values or 'undefined'.
1046
Ted Kremenek356e9d62009-07-22 04:35:42 +00001047#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001048 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001049#else
1050 if (R->hasStackStorage()) {
1051#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001052 // All stack variables are considered to have undefined values
1053 // upon creation. All heap allocated blocks are considered to
1054 // have undefined values as well unless they are explicitly bound
1055 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001056 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001057 }
1058
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001059 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001060 return SValuator::CastResult(state,
1061 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001062}
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001064std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001065RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001066 if (Optional<SVal> OV = getDirectBinding(B, R))
1067 if (const nonloc::LazyCompoundVal *V =
1068 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1069 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001071 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1072 const std::pair<const GRState *, const MemRegion *> &X =
1073 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001075 if (X.first)
1076 return std::make_pair(X.first,
1077 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001078 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001079 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1080 const std::pair<const GRState *, const MemRegion *> &X =
1081 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001083 if (X.first)
1084 return std::make_pair(X.first,
1085 MRMgr.getFieldRegionWithSuper(FR, X.second));
1086 }
1087
1088 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1089}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001090
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001091SVal RegionStoreManager::RetrieveElement(const GRState* state,
1092 const ElementRegion* R) {
1093 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001094 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001095 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001096 return *V;
1097
Ted Kremenek921109a2009-07-01 23:19:52 +00001098 const MemRegion* superR = R->getSuperRegion();
1099
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001100 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001101 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001102 // FIXME: Handle loads from strings where the literal is treated as
1103 // an integer, e.g., *((unsigned int*)"hello")
1104 ASTContext &Ctx = getContext();
1105 QualType T = StrR->getValueType(Ctx)->getAs<ArrayType>()->getElementType();
1106 if (T != Ctx.getCanonicalType(R->getElementType()))
1107 return UnknownVal();
1108
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001109 const StringLiteral *Str = StrR->getStringLiteral();
1110 SVal Idx = R->getIndex();
1111 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1112 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001113 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001114 if (i > byteLength) {
1115 // Buffer overflow checking in GRExprEngine should handle this case,
1116 // but we shouldn't rely on it to not overflow here if that checking
1117 // is disabled.
1118 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001119 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001120 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001121 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001122 }
1123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001125 // Special case: the current region represents a cast and it and the super
1126 // region both have pointer types or intptr_t types. If so, perform the
1127 // retrieve from the super region and appropriately "cast" the value.
1128 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001129 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001130 if (R->getIndex().isZeroConstant()) {
1131 if (const TypedRegion *superTR = dyn_cast<TypedRegion>(superR)) {
1132 ASTContext &Ctx = getContext();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001133 if (IsAnyPointerOrIntptr(superTR->getValueType(Ctx), Ctx)) {
1134 QualType valTy = R->getValueType(Ctx);
1135 if (IsAnyPointerOrIntptr(valTy, Ctx)) {
1136 // Retrieve the value from the super region. This will be casted to
1137 // valTy when we return to 'Retrieve'.
1138 const SValuator::CastResult &cr = Retrieve(state,
1139 loc::MemRegionVal(superR),
1140 valTy);
1141 return cr.getSVal();
1142 }
1143 }
1144 }
1145 }
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001146
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001147 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001148 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001149 if (SymbolRef parentSym = V->getAsSymbol())
1150 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001151
1152 if (V->isUnknownOrUndef())
1153 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001154
1155 // Handle LazyCompoundVals for the immediate super region. Other cases
1156 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001157 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001158 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001160 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1161 return RetrieveElement(LCV->getState(), R);
1162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremeneka6275a52009-07-15 02:31:43 +00001164 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001165 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001166 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001167
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001168 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001169}
1170
Mike Stump1eb44332009-09-09 15:08:12 +00001171SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001172 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001173
1174 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001175 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001176 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001177 return *V;
1178
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001179 QualType Ty = R->getValueType(getContext());
1180 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1181}
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001183SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1184 const TypedRegion *R,
1185 QualType Ty,
1186 const MemRegion *superR) {
1187
Mike Stump1eb44332009-09-09 15:08:12 +00001188 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001189 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001191 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001193 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001194 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001195 if (SymbolRef parentSym = D->getAsSymbol())
1196 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001198 if (D->isZeroConstant())
1199 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001201 if (D->isUnknown())
1202 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001204 assert(0 && "Unknown default value");
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001207 // If our super region is a field or element itself, walk up the region
1208 // hierarchy to see if there is a default value installed in an ancestor.
1209 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1210 superR = cast<SubRegion>(superR)->getSuperRegion();
1211 continue;
1212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001214 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001217 // Lazy binding?
1218 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001219 const MemRegion *lazyBindingRegion = NULL;
1220 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001222 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001223 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001225 if (isa<ElementRegion>(R))
1226 return RetrieveElement(lazyBindingState,
1227 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001229 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001230 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001231 }
1232
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001233 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001235 if (isa<ElementRegion>(R)) {
1236 // Currently we don't reason specially about Clang-style vectors. Check
1237 // if superR is a vector and if so return Unknown.
1238 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1239 if (typedSuperR->getValueType(getContext())->isVectorType())
1240 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001241 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001244 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001245 }
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001247 // All other values are symbolic.
1248 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001249}
Mike Stump1eb44332009-09-09 15:08:12 +00001250
1251SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001252 const ObjCIvarRegion* R) {
1253
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001254 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001255 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001256
Zhongxing Xu13d50172009-10-11 08:08:02 +00001257 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001258 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001260 const MemRegion *superR = R->getSuperRegion();
1261
1262 // Check if the super region has a binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001263 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001264 if (SymbolRef parentSym = V->getAsSymbol())
1265 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001267 // Other cases: give up.
1268 return UnknownVal();
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Ted Kremenek25c54572009-07-20 22:58:02 +00001271 return RetrieveLazySymbol(state, R);
1272}
1273
Ted Kremenek9031dd72009-07-21 00:12:07 +00001274SVal RegionStoreManager::RetrieveVar(const GRState *state,
1275 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremenek9031dd72009-07-21 00:12:07 +00001277 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001278 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Zhongxing Xu13d50172009-10-11 08:08:02 +00001280 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001281 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283 // Lazily derive a value for the VarRegion.
1284 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenek9031dd72009-07-21 00:12:07 +00001286 if (R->hasGlobalsOrParametersStorage())
1287 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Ted Kremenek9031dd72009-07-21 00:12:07 +00001289 return UndefinedVal();
1290}
1291
Mike Stump1eb44332009-09-09 15:08:12 +00001292SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001293 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Ted Kremenek25c54572009-07-20 22:58:02 +00001295 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001296
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001297 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001298 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001299}
1300
Mike Stump1eb44332009-09-09 15:08:12 +00001301SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1302 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001303 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001304 assert(T->isStructureType());
1305
Zhongxing Xub7507d12009-06-11 07:27:30 +00001306 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001307 RecordDecl* RD = RT->getDecl();
1308 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001309 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001310#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001311 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1312
Ted Kremenek67f28532009-06-17 22:02:04 +00001313 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1314 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001315 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001316
Douglas Gregore267ff32008-12-11 20:41:00 +00001317 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1318 FieldEnd = Fields.rend();
1319 Field != FieldEnd; ++Field) {
1320 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001321 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001322 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001323 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1324 }
1325
Zhongxing Xud91ee272009-06-23 09:02:15 +00001326 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001327#else
1328 return ValMgr.makeLazyCompoundVal(state, R);
1329#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001330}
1331
Ted Kremenek67f28532009-06-17 22:02:04 +00001332SVal RegionStoreManager::RetrieveArray(const GRState *state,
1333 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001334#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001335 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001336 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1337
1338 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001339 uint64_t size = CAT->getSize().getZExtValue();
1340 for (uint64_t i = 0; i < size; ++i) {
1341 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001342 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001343 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001344 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001345 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001346 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1347 }
1348
Zhongxing Xud91ee272009-06-23 09:02:15 +00001349 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001350#else
1351 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1352 return ValMgr.makeLazyCompoundVal(state, R);
1353#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001354}
1355
Ted Kremenek9af46f52009-06-16 22:36:44 +00001356//===----------------------------------------------------------------------===//
1357// Binding values to regions.
1358//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001359
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001360Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001361 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Ted Kremenek0964a062009-01-21 06:57:53 +00001363 if (isa<loc::MemRegionVal>(L))
1364 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Ted Kremenek0964a062009-01-21 06:57:53 +00001366 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001367 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001368 return RBFactory.Remove(B, R).getRoot();
1369 }
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Ted Kremenek0964a062009-01-21 06:57:53 +00001371 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001372}
1373
Ted Kremenek67f28532009-06-17 22:02:04 +00001374const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001375 if (isa<loc::ConcreteInt>(L))
1376 return state;
1377
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001379 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek9af46f52009-06-16 22:36:44 +00001381 // Check if the region is a struct region.
1382 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1383 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001384 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001386 // Special case: the current region represents a cast and it and the super
1387 // region both have pointer types or intptr_t types. If so, perform the
1388 // bind to the super region.
1389 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001390 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001391 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1392 if (ER->getIndex().isZeroConstant()) {
1393 if (const TypedRegion *superR =
1394 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1395 ASTContext &Ctx = getContext();
1396 QualType superTy = superR->getValueType(Ctx);
1397 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001398
1399 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001400 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001401 SValuator::CastResult cr =
1402 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001403 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1404 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001405 // For now, just invalidate the fields of the struct/union/class.
1406 // FIXME: Precisely handle the fields of the record.
1407 if (superTy->isRecordType())
1408 return InvalidateRegion(state, superR, NULL, 0);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001409 }
1410 }
1411 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001412 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1413 // Binding directly to a symbolic region should be treated as binding
1414 // to element 0.
1415 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001416 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001417 R = GetElementZeroRegion(SR, T);
1418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001420 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001421 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001422 return state->makeWithStore(
1423 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001424}
1425
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001426const GRState *RegionStoreManager::BindDecl(const GRState *ST,
1427 const VarDecl *VD,
1428 const LocationContext *LC,
1429 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001430
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001431 QualType T = VD->getType();
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001432 VarRegion* VR = MRMgr.getVarRegion(VD, LC);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001433
Ted Kremenek0964a062009-01-21 06:57:53 +00001434 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001435 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001436 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001437 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001438
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001439 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001440}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001441
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001442// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001443const GRState *
1444RegionStoreManager::BindCompoundLiteral(const GRState *state,
1445 const CompoundLiteralExpr* CL,
1446 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001448 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001449 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001450}
1451
Ted Kremenek67f28532009-06-17 22:02:04 +00001452const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001453 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001454 SVal Init) {
1455
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001456 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001457 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001458 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001459
Ted Kremenek46537392009-07-16 01:33:37 +00001460 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001461
1462 // Check if the init expr is a StringLiteral.
1463 if (isa<loc::MemRegionVal>(Init)) {
1464 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1465 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1466 const char* str = S->getStrData();
1467 unsigned len = S->getByteLength();
1468 unsigned j = 0;
1469
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001470 // Copy bytes from the string literal into the target array. Trailing bytes
1471 // in the array that are not covered by the string literal are initialized
1472 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001473 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001474 if (j >= len)
1475 break;
1476
Ted Kremenek46537392009-07-16 01:33:37 +00001477 SVal Idx = ValMgr.makeArrayIndex(i);
1478 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1479 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001480
Zhongxing Xud91ee272009-06-23 09:02:15 +00001481 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001482 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001483 }
1484
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001486 }
1487
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001488 // Handle lazy compound values.
1489 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1490 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001491
1492 // Remaining case: explicit compound values.
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001493 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001494 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001495 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Ted Kremenek46537392009-07-16 01:33:37 +00001497 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001498 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001499 if (VI == VE)
1500 break;
1501
Ted Kremenek46537392009-07-16 01:33:37 +00001502 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001503 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001504
1505 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001506 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001507 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001508 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001509 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001510 }
1511
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001512 // If the init list is shorter than the array length, set the array default
1513 // value.
Ted Kremenek46537392009-07-16 01:33:37 +00001514 if (i < size) {
Zhongxing Xue3a765f2009-06-24 00:56:31 +00001515 if (ElementTy->isIntegerType()) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001516 SVal V = ValMgr.makeZeroVal(ElementTy);
Zhongxing Xu13d50172009-10-11 08:08:02 +00001517 Store store = state->getStore();
1518 RegionBindings B = GetRegionBindings(store);
1519 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1520 state = state->makeWithStore(B.getRoot());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001521 }
1522 }
1523
Ted Kremenek67f28532009-06-17 22:02:04 +00001524 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001525}
1526
Ted Kremenek67f28532009-06-17 22:02:04 +00001527const GRState *
1528RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1529 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001530
Ted Kremenek67f28532009-06-17 22:02:04 +00001531 if (!Features.supportsFields())
1532 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001534 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001535 assert(T->isStructureType());
1536
Ted Kremenek6217b802009-07-29 21:53:49 +00001537 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001538 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001539
1540 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001541 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001542
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001543 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001544 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001545 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Ted Kremenek67f28532009-06-17 22:02:04 +00001547 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1548 // Ignore them and kill the field values.
1549 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001550 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001551
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001552 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001553 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001554
1555 RecordDecl::field_iterator FI, FE;
1556
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001558
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001559 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001560 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001561
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001562 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001563 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001564
Ted Kremenekcf549592009-09-22 21:19:14 +00001565 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001566 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001567 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001568 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001569 else
1570 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001571 }
1572
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001573 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001574 if (FI != FE) {
1575 Store store = state->getStore();
1576 RegionBindings B = GetRegionBindings(store);
1577 B = RBFactory.Add(B, R,
1578 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1579 state = state->makeWithStore(B.getRoot());
1580 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001581
Ted Kremenek67f28532009-06-17 22:02:04 +00001582 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001583}
1584
Zhongxing Xu13d50172009-10-11 08:08:02 +00001585Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1586 RegionBindings B = GetRegionBindings(store);
1587 llvm::OwningPtr<RegionStoreSubRegionMap>
1588 SubRegions(getRegionStoreSubRegionMap(store));
1589 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001590
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001591 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001592 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001593
Zhongxing Xu13d50172009-10-11 08:08:02 +00001594 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001595}
1596
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001597const GRState*
1598RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1599 const GRState *state,
1600 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001601
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001602 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001603 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001604
Mike Stump1eb44332009-09-09 15:08:12 +00001605 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001606 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001607
Mike Stump1eb44332009-09-09 15:08:12 +00001608 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001609 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001611 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1612 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001613 return state->makeWithStore(
1614 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001615}
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Ted Kremenek9af46f52009-06-16 22:36:44 +00001617//===----------------------------------------------------------------------===//
1618// State pruning.
1619//===----------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Ted Kremenek451ac092009-08-06 04:50:20 +00001621namespace {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001622class VISIBILITY_HIDDEN RBDNode
1623 : public std::pair<const GRState*, const MemRegion *> {
Ted Kremenek451ac092009-08-06 04:50:20 +00001624public:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001625 RBDNode(const GRState *st, const MemRegion *r)
1626 : std::pair<const GRState*, const MemRegion*>(st, r) {}
1627
1628 const GRState *getState() const { return first; }
1629 const MemRegion *getRegion() const { return second; }
1630};
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001632enum VisitFlag { NotVisited = 0, VisitedFromSubRegion, VisitedFromSuperRegion };
1633
1634class RBDItem : public RBDNode {
1635private:
1636 const VisitFlag VF;
1637
1638public:
1639 RBDItem(const GRState *st, const MemRegion *r, VisitFlag vf)
1640 : RBDNode(st, r), VF(vf) {}
1641
1642 VisitFlag getVisitFlag() const { return VF; }
Ted Kremenek451ac092009-08-06 04:50:20 +00001643};
1644} // end anonymous namespace
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001645
Mike Stump1eb44332009-09-09 15:08:12 +00001646void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001647 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001648 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001649{
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001650 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001651 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Ted Kremenek9af46f52009-06-16 22:36:44 +00001653 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001654 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001655 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001656
1657 // Do a pass over the regions in the store. For VarRegions we check if
1658 // the variable is still live and if so add it to the list of live roots.
1659 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001660 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001661
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001662 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001663 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001664 const MemRegion *R = I.getKey();
1665 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001666 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001667
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001668 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001669 // real roots.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001670 llvm::SmallVector<RBDItem, 10> WorkList;
1671 llvm::DenseMap<const MemRegion*,unsigned> IntermediateVisited;
1672
Ted Kremenek9af46f52009-06-16 22:36:44 +00001673 while (!IntermediateRoots.empty()) {
1674 const MemRegion* R = IntermediateRoots.back();
1675 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001676
1677 unsigned &visited = IntermediateVisited[R];
1678 if (visited)
1679 continue;
1680 visited = 1;
1681
Ted Kremenek9af46f52009-06-16 22:36:44 +00001682 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001683 if (SymReaper.isLive(Loc, VR->getDecl()))
1684 WorkList.push_back(RBDItem(&state, VR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001685 continue;
1686 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001687
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001688 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001689 if (SymReaper.isLive(SR->getSymbol()))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001690 WorkList.push_back(RBDItem(&state, SR, VisitedFromSuperRegion));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001691 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001692 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001693
1694 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001695 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001696 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001697 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001698 }
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001700 // Enqueue the RegionRoots onto WorkList.
1701 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1702 E=RegionRoots.end(); I!=E; ++I) {
1703 WorkList.push_back(RBDItem(&state, *I, VisitedFromSuperRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001704 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001705 RegionRoots.clear();
1706
1707 // Process the worklist.
1708 typedef llvm::DenseMap<std::pair<const GRState*, const MemRegion*>, VisitFlag>
1709 VisitMap;
1710
1711 VisitMap Visited;
1712
1713 while (!WorkList.empty()) {
1714 RBDItem N = WorkList.back();
1715 WorkList.pop_back();
1716
1717 // Have we visited this node before?
1718 VisitFlag &VF = Visited[N];
1719 if (VF >= N.getVisitFlag())
1720 continue;
1721
1722 const MemRegion *R = N.getRegion();
1723 const GRState *state_N = N.getState();
1724
1725 // Enqueue subregions?
1726 if (N.getVisitFlag() == VisitedFromSuperRegion) {
1727 RegionStoreSubRegionMap *M;
1728
1729 if (&state == state_N)
1730 M = SubRegions.get();
1731 else {
1732 RegionStoreSubRegionMap *& SM = SC[state_N];
1733 if (!SM)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001734 SM = getRegionStoreSubRegionMap(state_N->getStore());
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001735 M = SM;
1736 }
1737
1738 RegionStoreSubRegionMap::iterator I, E;
1739 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1740 WorkList.push_back(RBDItem(state_N, *I, VisitedFromSuperRegion));
1741 }
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001743 // At this point, if we have already visited this region before, we are
1744 // done.
1745 if (VF != NotVisited) {
1746 VF = N.getVisitFlag();
1747 continue;
1748 }
1749 VF = N.getVisitFlag();
1750
1751 // Enqueue the super region.
1752 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1753 const MemRegion *superR = SR->getSuperRegion();
1754 if (!isa<MemSpaceRegion>(superR)) {
1755 // If 'R' is a field or an element, we want to keep the bindings
1756 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001757 // pointer arithmetic can get us to the other fields or elements.
1758 // FIXME: add an assertion that this is always true.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001759 VisitFlag NewVisit =
1760 isa<FieldRegion>(R) || isa<ElementRegion>(R) || isa<ObjCIvarRegion>(R)
1761 ? VisitedFromSuperRegion : VisitedFromSubRegion;
1762
1763 WorkList.push_back(RBDItem(state_N, superR, NewVisit));
1764 }
1765 }
1766
1767 // Mark the symbol for any live SymbolicRegion as "live". This means we
1768 // should continue to track that symbol.
1769 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1770 SymReaper.markLive(SymR->getSymbol());
1771
1772 Store store_N = state_N->getStore();
1773 RegionBindings B_N = GetRegionBindings(store_N);
1774
1775 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001776 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001777
Zhongxing Xu13d50172009-10-11 08:08:02 +00001778 if (V) {
1779 // Check for lazy bindings.
1780 if (const nonloc::LazyCompoundVal *LCV =
1781 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001782
Zhongxing Xu13d50172009-10-11 08:08:02 +00001783 const LazyCompoundValData *D = LCV->getCVData();
1784 WorkList.push_back(RBDItem(D->getState(), D->getRegion(),
1785 VisitedFromSuperRegion));
1786 }
1787 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001788 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001789 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001790 SI!=SE;++SI)
1791 SymReaper.markLive(*SI);
1792
Zhongxing Xu13d50172009-10-11 08:08:02 +00001793 // If V is a region, then add it to the worklist.
1794 if (const MemRegion *RX = V->getAsRegion())
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001795 WorkList.push_back(RBDItem(state_N, RX, VisitedFromSuperRegion));
1796 }
1797 }
1798 }
1799
Ted Kremenek9af46f52009-06-16 22:36:44 +00001800 // We have now scanned the store, marking reachable regions and symbols
1801 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001802 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001803 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001804 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001805 // If this region live? Is so, none of its symbols are dead.
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001806 if (Visited.find(std::make_pair(&state, R)) != Visited.end())
Ted Kremenek9af46f52009-06-16 22:36:44 +00001807 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Ted Kremenek9af46f52009-06-16 22:36:44 +00001809 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001810 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Ted Kremenek9af46f52009-06-16 22:36:44 +00001812 // Mark all non-live symbols that this region references as dead.
1813 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1814 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Zhongxing Xu13d50172009-10-11 08:08:02 +00001816 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001817 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1818 for (; SI != SE; ++SI)
1819 SymReaper.maybeDead(*SI);
1820 }
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001822 // Write the store back.
1823 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001824}
1825
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001826GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1827 StackFrameContext const *frame) {
1828 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1829 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1830
1831 FunctionDecl::param_const_iterator PI = FD->param_begin();
1832
1833 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1834
1835 // Copy the arg expression value to the arg variables.
1836 for (; AI != AE; ++AI, ++PI) {
1837 SVal ArgVal = state->getSVal(*AI);
1838 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1839 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1840 }
1841
1842 return state;
1843}
1844
Ted Kremenek9af46f52009-06-16 22:36:44 +00001845//===----------------------------------------------------------------------===//
1846// Utility methods.
1847//===----------------------------------------------------------------------===//
1848
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001849void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001850 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001851 RegionBindings B = GetRegionBindings(store);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001852 OS << "Store (direct bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Ted Kremenek451ac092009-08-06 04:50:20 +00001854 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001855 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001856}