blob: 2e62395db87cd208b168b98e0472eb84020e2cd1 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
Zhongxing Xu0b143312009-08-21 13:25:15 +000018#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000019#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000020#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000024
25#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000027#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000028#include "llvm/Support/Compiler.h"
29
30using namespace clang;
31
Ted Kremenek356e9d62009-07-22 04:35:42 +000032#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000033#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000034
Zhongxing Xu13d50172009-10-11 08:08:02 +000035namespace {
36class BindingVal {
37public:
38 enum BindingKind { Direct, Default };
39private:
40 SVal Value;
41 BindingKind Kind;
42
43public:
44 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
45
46 bool isDefault() const { return Kind == Default; }
47
48 const SVal *getValue() const { return &Value; }
49
50 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
51
52 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
53
54 void Profile(llvm::FoldingSetNodeID& ID) const {
55 Value.Profile(ID);
56 ID.AddInteger(Kind);
57 }
58
59 inline bool operator==(const BindingVal& R) const {
60 return Value == R.Value && Kind == R.Kind;
61 }
62
63 inline bool operator!=(const BindingVal& R) const {
64 return !(*this == R);
65 }
66};
67}
68
69namespace llvm {
70static inline
71llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
72 if (V.isDefault())
73 os << "(default) ";
74 else
75 os << "(direct) ";
76 os << *V.getValue();
77 return os;
78}
79} // end llvm namespace
80
Zhongxing Xubaf03a72008-11-24 09:44:56 +000081// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000082typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000083
Ted Kremenek50dc1b32008-12-24 01:05:03 +000084//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000085// Fine-grained control of RegionStoreManager.
86//===----------------------------------------------------------------------===//
87
88namespace {
89struct VISIBILITY_HIDDEN minimal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000090struct VISIBILITY_HIDDEN maximal_features_tag {};
91
Ted Kremenek9af46f52009-06-16 22:36:44 +000092class VISIBILITY_HIDDEN RegionStoreFeatures {
93 bool SupportsFields;
94 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenek9af46f52009-06-16 22:36:44 +000096public:
97 RegionStoreFeatures(minimal_features_tag) :
98 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek9af46f52009-06-16 22:36:44 +0000100 RegionStoreFeatures(maximal_features_tag) :
101 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek9af46f52009-06-16 22:36:44 +0000105 bool supportsFields() const { return SupportsFields; }
106 bool supportsRemaining() const { return SupportsRemaining; }
107};
108}
109
110//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000111// Region "Extents"
112//===----------------------------------------------------------------------===//
113//
114// MemRegions represent chunks of memory with a size (their "extent"). This
115// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000116//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117namespace { class VISIBILITY_HIDDEN RegionExtents {}; }
118static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000119namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000120 template<> struct GRStateTrait<RegionExtents>
121 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
122 static void* GDMIndex() { return &RegionExtentsIndex; }
123 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000124}
125
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000126//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000127// Utility functions.
128//===----------------------------------------------------------------------===//
129
130static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
131 if (ty->isAnyPointerType())
132 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000134 return ty->isIntegerType() && ty->isScalarType() &&
135 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
136}
137
138//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000139// Main RegionStore logic.
140//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000141
Zhongxing Xu17892752008-10-08 02:50:44 +0000142namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144class VISIBILITY_HIDDEN RegionStoreSubRegionMap : public SubRegionMap {
145 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000146 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000147 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000150 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000151 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000152
153 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000154 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000155 return true;
156 }
157
158 I->second = F.Add(I->second, SubRegion);
159 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000160 }
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000162 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek59e8f112009-03-03 01:35:36 +0000164 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremenek5dc27462009-03-03 02:51:43 +0000166 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000167 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000168
169 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000170 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremenek59e8f112009-03-03 01:35:36 +0000172 llvm::ImmutableSet<const MemRegion*> S = I->second;
173 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
174 SI != SE; ++SI) {
175 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000176 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000177 }
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Ted Kremenek5dc27462009-03-03 02:51:43 +0000179 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000180 }
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000182 typedef SetTy::iterator iterator;
183
184 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
185 Map::iterator I = M.find(R);
186 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
187 return std::make_pair(S.begin(), S.end());
188 }
Mike Stump1eb44332009-09-09 15:08:12 +0000189};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000190
Zhongxing Xu17892752008-10-08 02:50:44 +0000191class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000192 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000193 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000194
195 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
196 SMCache SC;
197
Zhongxing Xu17892752008-10-08 02:50:44 +0000198public:
Mike Stump1eb44332009-09-09 15:08:12 +0000199 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000200 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000201 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000202 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000203
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000204 virtual ~RegionStoreManager() {
205 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
206 delete (*I).second;
207 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000208
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000209 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Zhongxing Xu13d50172009-10-11 08:08:02 +0000211 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Zhongxing Xu13d50172009-10-11 08:08:02 +0000213 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
214 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000215 /// getDefaultBinding - Returns an SVal* representing an optional default
216 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000217 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000218
219 /// setImplicitDefaultValue - Set the default binding for the provided
220 /// MemRegion to the value implicitly defined for compound literals when
221 /// the value is not specified.
222 const GRState *setImplicitDefaultValue(const GRState *state,
223 const MemRegion *R,
224 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000226 /// getLValueString - Returns an SVal representing the lvalue of a
227 /// StringLiteral. Within RegionStore a StringLiteral has an
228 /// associated StringRegion, and the lvalue of a StringLiteral is
229 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000230 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000231
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000232 /// getLValueCompoundLiteral - Returns an SVal representing the
233 /// lvalue of a compound literal. Within RegionStore a compound
234 /// literal has an associated region, and the lvalue of the
235 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000236 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000237
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000238 /// getLValueVar - Returns an SVal that represents the lvalue of a
239 /// variable. Within RegionStore a variable has an associated
240 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000241 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000243 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000244
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000245 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000247 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000248
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000249 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000250
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000251
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000252 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
253 /// type. 'Array' represents the lvalue of the array being decayed
254 /// to a pointer, and the returned SVal represents the decayed
255 /// version of that lvalue (i.e., a pointer to the first element of
256 /// the array). This is called by GRExprEngine when evaluating
257 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000258 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000259
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000260 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000261 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000262
Mike Stump1eb44332009-09-09 15:08:12 +0000263 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000264 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000265 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000266
Ted Kremenek67f28532009-06-17 22:02:04 +0000267 //===-------------------------------------------------------------------===//
268 // Binding values to regions.
269 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000270
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000271 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000272 const Expr *E, unsigned Count,
273 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000275private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000276 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000277 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
279public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000280 const GRState *Bind(const GRState *state, Loc LV, SVal V);
281
282 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000283 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000285 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
286 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000287
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000288 const GRState *BindDeclWithNoInit(const GRState *state,
289 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000290 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000291 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000292
Ted Kremenek67f28532009-06-17 22:02:04 +0000293 /// BindStruct - Bind a compound value to a structure.
294 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenek67f28532009-06-17 22:02:04 +0000296 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000297
298 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000299 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000300
Ted Kremenek67f28532009-06-17 22:02:04 +0000301 Store Remove(Store store, Loc LV);
302
303 //===------------------------------------------------------------------===//
304 // Loading values from regions.
305 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000306
Ted Kremenek67f28532009-06-17 22:02:04 +0000307 /// The high level logic for this method is this:
308 /// Retrieve (L)
309 /// if L has binding
310 /// return L's binding
311 /// else if L is in killset
312 /// return unknown
313 /// else
314 /// if L is on stack or heap
315 /// return undefined
316 /// else
317 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000318 SValuator::CastResult Retrieve(const GRState *state, Loc L,
319 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000320
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000321 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000322
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000323 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000325 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Ted Kremenek9031dd72009-07-21 00:12:07 +0000327 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremenek25c54572009-07-20 22:58:02 +0000329 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000331 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
332 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek67f28532009-06-17 22:02:04 +0000334 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000335 /// struct copy:
336 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000337 /// x = y;
338 /// y's value is retrieved by this method.
339 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000343 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000344 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000346 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
347 const GRState *state,
348 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000349
Ted Kremenek0954cde2009-09-24 04:11:44 +0000350 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
351 QualType T);
352
Ted Kremenek67f28532009-06-17 22:02:04 +0000353 //===------------------------------------------------------------------===//
354 // State pruning.
355 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Ted Kremenek67f28532009-06-17 22:02:04 +0000357 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
358 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000359 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
361
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000362 const GRState *EnterStackFrame(const GRState *state,
363 const StackFrameContext *frame);
364
Ted Kremenek67f28532009-06-17 22:02:04 +0000365 //===------------------------------------------------------------------===//
366 // Region "extents".
367 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Ted Kremenek67f28532009-06-17 22:02:04 +0000369 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000370 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
371 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000372
373 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000374 // Utility methods.
375 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Ted Kremenek451ac092009-08-06 04:50:20 +0000377 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000378 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000379 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000380
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000381 void print(Store store, llvm::raw_ostream& Out, const char* nl,
382 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000383
384 void iterBindings(Store store, BindingsHandler& f) {
385 // FIXME: Implement.
386 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000387
Ted Kremenek67f28532009-06-17 22:02:04 +0000388 // FIXME: Remove.
389 BasicValueFactory& getBasicVals() {
390 return StateMgr.getBasicVals();
391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Ted Kremenek67f28532009-06-17 22:02:04 +0000393 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000394 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000395};
396
397} // end anonymous namespace
398
Ted Kremenek9af46f52009-06-16 22:36:44 +0000399//===----------------------------------------------------------------------===//
400// RegionStore creation.
401//===----------------------------------------------------------------------===//
402
403StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
404 RegionStoreFeatures F = maximal_features_tag();
405 return new RegionStoreManager(StMgr, F);
406}
407
408StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
409 RegionStoreFeatures F = minimal_features_tag();
410 F.enableFields(true);
411 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000412}
413
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000414void
415RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000416 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000417 const MemRegion *superR = R->getSuperRegion();
418 if (add(superR, R))
419 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000420 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000421}
422
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000423RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000424RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
425 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000426 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000428 llvm::SmallVector<const SubRegion*, 10> WL;
429
Ted Kremenek451ac092009-08-06 04:50:20 +0000430 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000431 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
432 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Mike Stump1eb44332009-09-09 15:08:12 +0000434 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000435 // don't have direct bindings but are super regions of those that do.
436 while (!WL.empty()) {
437 const SubRegion *R = WL.back();
438 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000439 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000440 }
441
Ted Kremenek14453bf2009-03-03 19:02:42 +0000442 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000443}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000444
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000445SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000446 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447}
448
Ted Kremenek9af46f52009-06-16 22:36:44 +0000449//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000450// Binding invalidation.
451//===----------------------------------------------------------------------===//
452
Zhongxing Xu13d50172009-10-11 08:08:02 +0000453void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
454 const MemRegion *R,
455 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000456 RegionStoreSubRegionMap::iterator I, E;
457
458 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000459 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000461 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000462}
463
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000464const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
465 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000466 const Expr *Ex,
Ted Kremenek473e1672009-10-16 00:30:49 +0000467 unsigned Count,
468 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000469 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000471 // Strip away casts.
Zhongxing Xu479529e2009-11-10 02:17:20 +0000472 R = R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000473
Ted Kremenek87806792009-09-27 20:45:21 +0000474 // Get the mapping of regions -> subregions.
475 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000476 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000477
478 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000479
Ted Kremenek87806792009-09-27 20:45:21 +0000480 llvm::DenseMap<const MemRegion *, unsigned> Visited;
481 llvm::SmallVector<const MemRegion *, 10> WorkList;
482 WorkList.push_back(R);
483
484 while (!WorkList.empty()) {
485 R = WorkList.back();
486 WorkList.pop_back();
487
488 // Have we visited this region before?
489 unsigned &visited = Visited[R];
490 if (visited)
491 continue;
492 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Ted Kremenek87806792009-09-27 20:45:21 +0000494 // Add subregions to work list.
495 RegionStoreSubRegionMap::iterator I, E;
496 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
497 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000498
499 // Get the old binding. Is it a region? If so, add it to the worklist.
500 if (Optional<SVal> V = getDirectBinding(B, R)) {
501 if (const MemRegion *RV = V->getAsRegion())
502 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000503
504 // A symbol? Mark it touched by the invalidation.
505 if (IS) {
506 if (SymbolRef Sym = V->getAsSymbol())
507 IS->insert(Sym);
508 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000509 }
510
Ted Kremenek473e1672009-10-16 00:30:49 +0000511 // Symbolic region? Mark that symbol touched by the invalidation.
512 if (IS) {
513 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
514 IS->insert(SR->getSymbol());
515 }
516
517 // Handle the region itself.
Ted Kremenek87806792009-09-27 20:45:21 +0000518 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
519 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000520 // Invalidate the region by setting its default value to
521 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000522 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
523 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000524 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000525 continue;
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Ted Kremenek87806792009-09-27 20:45:21 +0000528 if (!R->isBoundable())
529 continue;
530
531 const TypedRegion *TR = cast<TypedRegion>(R);
532 QualType T = TR->getValueType(Ctx);
533
534 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000535 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
536
Zhongxing Xu13d50172009-10-11 08:08:02 +0000537 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000538 if (!RD)
539 continue;
540
Zhongxing Xu13d50172009-10-11 08:08:02 +0000541 // Invalidate the region by setting its default value to
542 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000543 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
544 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000545 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000546 continue;
547 }
548
549 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
550 // Set the default value of the array to conjured symbol.
551 DefinedOrUnknownSVal V =
552 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000553 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000554 continue;
555 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000556
557 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
558 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000559 // For fields and elements whose super region has also been invalidated,
560 // only remove the old binding. The super region will get set with a
561 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000562 B = RBFactory.Remove(B, R);
563 continue;
564 }
Ted Kremenek87806792009-09-27 20:45:21 +0000565
Ted Kremenek389c44c2009-09-29 03:12:50 +0000566 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000567 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
568 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000569 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000570 }
571
Ted Kremenek87806792009-09-27 20:45:21 +0000572 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000573 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000574}
575
576//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000577// getLValueXXX methods.
578//===----------------------------------------------------------------------===//
579
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000580/// getLValueString - Returns an SVal representing the lvalue of a
581/// StringLiteral. Within RegionStore a StringLiteral has an
582/// associated StringRegion, and the lvalue of a StringLiteral is the
583/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000584SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000585 return loc::MemRegionVal(MRMgr.getStringRegion(S));
586}
587
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000588/// getLValueVar - Returns an SVal that represents the lvalue of a
589/// variable. Within RegionStore a variable has an associated
590/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000591SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000592 const LocationContext *LC) {
593 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000594}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000595
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000596/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
597/// of a compound literal. Within RegionStore a compound literal
598/// has an associated region, and the lvalue of the compound literal
599/// is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000600SVal
601RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000602 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
603}
604
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000605SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
606 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000607}
608
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000609SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
610 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000611}
612
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000613SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000614 if (Base.isUnknownOrUndef())
615 return Base;
616
617 Loc BaseL = cast<Loc>(Base);
618 const MemRegion* BaseR = 0;
619
620 switch (BaseL.getSubKind()) {
621 case loc::MemRegionKind:
622 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
623 break;
624
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000625 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000626 // These are anormal cases. Flag an undefined value.
627 return UndefinedVal();
628
629 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000630 // While these seem funny, this can happen through casts.
631 // FIXME: What we should return is the field offset. For example,
632 // add the field offset to the integer value. That way funny things
633 // like this work properly: &(((struct foo *) 0xa)->f)
634 return Base;
635
636 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000637 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000638 return Base;
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000641 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
642 // of FieldDecl.
643 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
644 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000645
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000646 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000647}
648
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000649SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
650 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000651
Ted Kremenekde7ec632009-03-09 22:44:49 +0000652 // If the base is an unknown or undefined value, just return it back.
653 // FIXME: For absolute pointer addresses, we just return that value back as
654 // well, although in reality we should return the offset added to that
655 // value.
656 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000657 return Base;
658
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000659 // Only handle integer offsets... for now.
660 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000661 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000662
Zhongxing Xuce760782009-05-09 13:20:07 +0000663 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000664
665 // Pointer of any type can be cast and used as array base.
666 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Ted Kremenek46537392009-07-16 01:33:37 +0000668 // Convert the offset to the appropriate size and signedness.
669 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000671 if (!ElemR) {
672 //
673 // If the base region is not an ElementRegion, create one.
674 // This can happen in the following example:
675 //
676 // char *p = __builtin_alloc(10);
677 // p[1] = 8;
678 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000679 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000680 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000681 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000682 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000685 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000687 if (!isa<nonloc::ConcreteInt>(BaseIdx))
688 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000690 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
691 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
692 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Ted Kremenek46537392009-07-16 01:33:37 +0000694 // Compute the new index.
695 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Ted Kremenek46537392009-07-16 01:33:37 +0000697 // Construct the new ElementRegion.
698 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000699 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000700 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000701}
702
Ted Kremenek9af46f52009-06-16 22:36:44 +0000703//===----------------------------------------------------------------------===//
704// Extents for regions.
705//===----------------------------------------------------------------------===//
706
Zhongxing Xue884ff82009-11-12 02:48:32 +0000707DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
708 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000710 switch (R->getKind()) {
711 case MemRegion::MemSpaceRegionKind:
712 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000713 return UnknownVal();
714
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000715 case MemRegion::FunctionTextRegionKind:
716 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000717 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000718 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000719 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000720
721 // Not yet handled.
722 case MemRegion::AllocaRegionKind:
723 case MemRegion::CompoundLiteralRegionKind:
724 case MemRegion::ElementRegionKind:
725 case MemRegion::FieldRegionKind:
726 case MemRegion::ObjCIvarRegionKind:
727 case MemRegion::ObjCObjectRegionKind:
728 case MemRegion::SymbolicRegionKind:
729 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000731 case MemRegion::StringRegionKind: {
732 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000733 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000734 // operations with signed indices.
735 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000738 case MemRegion::VarRegionKind: {
739 const VarRegion* VR = cast<VarRegion>(R);
740 // Get the type of the variable.
741 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000743 // FIXME: Handle variable-length arrays.
744 if (isa<VariableArrayType>(T))
745 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000747 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
748 // return the size as signed integer.
749 return ValMgr.makeIntVal(CAT->getSize(), false);
750 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000751
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000752 // Clients can use ordinary variables as if they were arrays. These
753 // essentially are arrays of size 1.
754 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000757 case MemRegion::BEG_DECL_REGIONS:
758 case MemRegion::END_DECL_REGIONS:
759 case MemRegion::BEG_TYPED_REGIONS:
760 case MemRegion::END_TYPED_REGIONS:
761 assert(0 && "Infeasible region");
762 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000765 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000766 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000767}
768
Ted Kremenek67f28532009-06-17 22:02:04 +0000769const GRState *RegionStoreManager::setExtent(const GRState *state,
770 const MemRegion *region,
771 SVal extent) {
772 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000773}
774
775//===----------------------------------------------------------------------===//
776// Location and region casting.
777//===----------------------------------------------------------------------===//
778
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000779/// ArrayToPointer - Emulates the "decay" of an array to a pointer
780/// type. 'Array' represents the lvalue of the array being decayed
781/// to a pointer, and the returned SVal represents the decayed
782/// version of that lvalue (i.e., a pointer to the first element of
783/// the array). This is called by GRExprEngine when evaluating casts
784/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000785SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000786 if (!isa<loc::MemRegionVal>(Array))
787 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Ted Kremenekabb042f2008-12-13 19:24:37 +0000789 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
790 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000792 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000793 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000795 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000796 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000797 ArrayType *AT = cast<ArrayType>(T);
798 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Ted Kremenek75185b52009-07-16 00:00:11 +0000800 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
801 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000802
803 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000804}
805
Ted Kremenek9af46f52009-06-16 22:36:44 +0000806//===----------------------------------------------------------------------===//
807// Pointer arithmetic.
808//===----------------------------------------------------------------------===//
809
Mike Stump1eb44332009-09-09 15:08:12 +0000810SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000811 BinaryOperator::Opcode Op, Loc L, NonLoc R,
812 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000813 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000814 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000815 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000816
Zhongxing Xua1718c72009-04-03 07:33:13 +0000817 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000818 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000819
Ted Kremenek3bccf082009-07-11 00:58:27 +0000820 switch (MR->getKind()) {
821 case MemRegion::SymbolicRegionKind: {
822 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000823 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000824 QualType T = Sym->getType(getContext());
825 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000827 if (const PointerType *PT = T->getAs<PointerType>())
828 EleTy = PT->getPointeeType();
829 else
John McCall183700f2009-09-21 23:43:11 +0000830 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek3bccf082009-07-11 00:58:27 +0000832 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
833 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000834 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000835 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000837 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000838 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000839 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000840 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
841 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000842 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000843 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000844
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 case MemRegion::ElementRegionKind: {
846 ER = cast<ElementRegion>(MR);
847 break;
848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Ted Kremenek3bccf082009-07-11 00:58:27 +0000850 // Not yet handled.
851 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000852 case MemRegion::StringRegionKind: {
853
854 }
855 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000856 case MemRegion::CompoundLiteralRegionKind:
857 case MemRegion::FieldRegionKind:
858 case MemRegion::ObjCObjectRegionKind:
859 case MemRegion::ObjCIvarRegionKind:
860 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000861
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000862 case MemRegion::FunctionTextRegionKind:
863 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000864 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000865 // Technically this can happen if people do funny things with casts.
866 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Ted Kremenek3bccf082009-07-11 00:58:27 +0000868 case MemRegion::MemSpaceRegionKind:
869 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
870 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Ted Kremenek3bccf082009-07-11 00:58:27 +0000872 case MemRegion::BEG_DECL_REGIONS:
873 case MemRegion::END_DECL_REGIONS:
874 case MemRegion::BEG_TYPED_REGIONS:
875 case MemRegion::END_TYPED_REGIONS:
876 assert(0 && "Infeasible region");
877 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000878 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000879
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000880 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000881 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000882
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000883 // For now, only support:
884 // (a) concrete integer indices that can easily be resolved
885 // (b) 0 + symbolic index
886 if (Base) {
887 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
888 // FIXME: Should use SValuator here.
889 SVal NewIdx =
890 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000891 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000892 const MemRegion* NewER =
893 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
894 ER->getSuperRegion(), getContext());
895 return ValMgr.makeLoc(NewER);
896 }
897 if (0 == Base->getValue()) {
898 const MemRegion* NewER =
899 MRMgr.getElementRegion(ER->getElementType(), R,
900 ER->getSuperRegion(), getContext());
901 return ValMgr.makeLoc(NewER);
902 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Ted Kremenek5dc27462009-03-03 02:51:43 +0000905 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000906}
907
Ted Kremenek9af46f52009-06-16 22:36:44 +0000908//===----------------------------------------------------------------------===//
909// Loading values from regions.
910//===----------------------------------------------------------------------===//
911
Zhongxing Xu13d50172009-10-11 08:08:02 +0000912Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
913 const MemRegion *R) {
914 if (const BindingVal *BV = B.lookup(R))
915 return Optional<SVal>::create(BV->getDirectValue());
916
917 return Optional<SVal>();
918}
919
920Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000921 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000923 if (R->isBoundable())
924 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
925 if (TR->getValueType(getContext())->isUnionType())
926 return UnknownVal();
927
Zhongxing Xu13d50172009-10-11 08:08:02 +0000928 if (BindingVal const *V = B.lookup(R))
929 return Optional<SVal>::create(V->getDefaultValue());
930
931 return Optional<SVal>();
932}
933
934Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
935 const MemRegion *R) {
936 if (const BindingVal *BV = B.lookup(R))
937 return Optional<SVal>::create(BV->getValue());
938
939 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000940}
941
Ted Kremeneka6275a52009-07-15 02:31:43 +0000942static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
943 RTy = Ctx.getCanonicalType(RTy);
944 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Ted Kremeneka6275a52009-07-15 02:31:43 +0000946 if (RTy == UsedTy)
947 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000948
949
Ted Kremenek25c54572009-07-20 22:58:02 +0000950 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000951 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000952 // represents a reinterpretation.
953 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000954 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000955 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000956
957 return PUsedTy && PRTy &&
958 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000959 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000960 }
961
962 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000963}
964
Ted Kremenek0954cde2009-09-24 04:11:44 +0000965const ElementRegion *
966RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
967 ASTContext &Ctx = getContext();
968 SVal idx = ValMgr.makeZeroArrayIndex();
969 assert(!T.isNull());
970 return MRMgr.getElementRegion(T, idx, SR, Ctx);
971}
972
973
974
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000975SValuator::CastResult
976RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000977
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000978 assert(!isa<UnknownVal>(L) && "location unknown");
979 assert(!isa<UndefinedVal>(L) && "location undefined");
980
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000981 // FIXME: Is this even possible? Shouldn't this be treated as a null
982 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000983 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000984 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000985
Ted Kremenek67f28532009-06-17 22:02:04 +0000986 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000987
Zhongxing Xu91844122009-05-20 09:18:48 +0000988 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000989 // Example:
990 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000991 // char* p = alloca();
992 // read(p);
993 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000994 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000995 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Ted Kremenek0954cde2009-09-24 04:11:44 +0000997 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
998 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Ted Kremenek968f0a62009-08-03 21:41:46 +00001000 if (isa<CodeTextRegion>(MR))
1001 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001003 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1004 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001005 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001006 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001007
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001008 // FIXME: We should eventually handle funny addressing. e.g.:
1009 //
1010 // int x = ...;
1011 // int *p = &x;
1012 // char *q = (char*) p;
1013 // char c = *q; // returns the first byte of 'x'.
1014 //
1015 // Such funny addressing will occur due to layering of regions.
1016
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001017#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001018 ASTContext &Ctx = getContext();
1019 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001020 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1021 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001022 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001023 assert(Ctx.getCanonicalType(RTy) ==
1024 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001025 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001026#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001027
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001028 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001029 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001031 // FIXME: Handle unions.
1032 if (RTy->isUnionType())
1033 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001034
1035 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001036 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001037
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001038 // FIXME: handle Vector types.
1039 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001040 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001041
1042 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001043 return SValuator::CastResult(state,
1044 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001045
1046 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001047 return SValuator::CastResult(state,
1048 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek25c54572009-07-20 22:58:02 +00001050 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001051 return SValuator::CastResult(state,
1052 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Ted Kremenek9031dd72009-07-21 00:12:07 +00001054 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001055 return SValuator::CastResult(state,
1056 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001057
Ted Kremenek451ac092009-08-06 04:50:20 +00001058 RegionBindings B = GetRegionBindings(state->getStore());
1059 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001060
1061 // Check if the region has a binding.
1062 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001063 if (SVal const *SV = V->getValue())
1064 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001065
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001066 // The location does not have a bound value. This means that it has
1067 // the value it had upon its creation and/or entry to the analyzed
1068 // function/method. These are either symbolic values or 'undefined'.
1069
Ted Kremenek356e9d62009-07-22 04:35:42 +00001070#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001071 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001072#else
1073 if (R->hasStackStorage()) {
1074#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001075 // All stack variables are considered to have undefined values
1076 // upon creation. All heap allocated blocks are considered to
1077 // have undefined values as well unless they are explicitly bound
1078 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001079 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001080 }
1081
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001082 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001083 return SValuator::CastResult(state,
1084 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001085}
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001087std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001088RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001089 if (Optional<SVal> OV = getDirectBinding(B, R))
1090 if (const nonloc::LazyCompoundVal *V =
1091 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1092 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001094 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1095 const std::pair<const GRState *, const MemRegion *> &X =
1096 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001098 if (X.first)
1099 return std::make_pair(X.first,
1100 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001101 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001102 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1103 const std::pair<const GRState *, const MemRegion *> &X =
1104 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001106 if (X.first)
1107 return std::make_pair(X.first,
1108 MRMgr.getFieldRegionWithSuper(FR, X.second));
1109 }
1110
1111 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1112}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001113
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001114SVal RegionStoreManager::RetrieveElement(const GRState* state,
1115 const ElementRegion* R) {
1116 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001117 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001118 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001119 return *V;
1120
Ted Kremenek921109a2009-07-01 23:19:52 +00001121 const MemRegion* superR = R->getSuperRegion();
1122
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001123 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001124 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001125 // FIXME: Handle loads from strings where the literal is treated as
1126 // an integer, e.g., *((unsigned int*)"hello")
1127 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001128 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001129 if (T != Ctx.getCanonicalType(R->getElementType()))
1130 return UnknownVal();
1131
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001132 const StringLiteral *Str = StrR->getStringLiteral();
1133 SVal Idx = R->getIndex();
1134 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1135 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001136 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001137 if (i > byteLength) {
1138 // Buffer overflow checking in GRExprEngine should handle this case,
1139 // but we shouldn't rely on it to not overflow here if that checking
1140 // is disabled.
1141 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001142 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001143 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001144 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001145 }
1146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001148 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001149 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001150 if (SymbolRef parentSym = V->getAsSymbol())
1151 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001152
1153 if (V->isUnknownOrUndef())
1154 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001155
1156 // Handle LazyCompoundVals for the immediate super region. Other cases
1157 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001158 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001159 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001161 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1162 return RetrieveElement(LCV->getState(), R);
1163 }
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Ted Kremeneka6275a52009-07-15 02:31:43 +00001165 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001166 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001167 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001168
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001169 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001170}
1171
Mike Stump1eb44332009-09-09 15:08:12 +00001172SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001173 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001174
1175 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001176 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001177 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001178 return *V;
1179
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001180 QualType Ty = R->getValueType(getContext());
1181 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1182}
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001184SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1185 const TypedRegion *R,
1186 QualType Ty,
1187 const MemRegion *superR) {
1188
Mike Stump1eb44332009-09-09 15:08:12 +00001189 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001190 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001192 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001194 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001195 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001196 if (SymbolRef parentSym = D->getAsSymbol())
1197 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001199 if (D->isZeroConstant())
1200 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001202 if (D->isUnknown())
1203 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001205 assert(0 && "Unknown default value");
1206 }
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001208 // If our super region is a field or element itself, walk up the region
1209 // hierarchy to see if there is a default value installed in an ancestor.
1210 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1211 superR = cast<SubRegion>(superR)->getSuperRegion();
1212 continue;
1213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001215 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001218 // Lazy binding?
1219 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001220 const MemRegion *lazyBindingRegion = NULL;
1221 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001223 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001224 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001226 if (isa<ElementRegion>(R))
1227 return RetrieveElement(lazyBindingState,
1228 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001230 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001231 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001232 }
1233
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001234 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001236 if (isa<ElementRegion>(R)) {
1237 // Currently we don't reason specially about Clang-style vectors. Check
1238 // if superR is a vector and if so return Unknown.
1239 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1240 if (typedSuperR->getValueType(getContext())->isVectorType())
1241 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001242 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001245 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001246 }
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001248 // All other values are symbolic.
1249 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001250}
Mike Stump1eb44332009-09-09 15:08:12 +00001251
1252SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001253 const ObjCIvarRegion* R) {
1254
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001255 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001256 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001257
Zhongxing Xu13d50172009-10-11 08:08:02 +00001258 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001259 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001261 const MemRegion *superR = R->getSuperRegion();
1262
Ted Kremenekab22ee92009-10-20 01:20:57 +00001263 // Check if the super region has a default binding.
1264 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001265 if (SymbolRef parentSym = V->getAsSymbol())
1266 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001268 // Other cases: give up.
1269 return UnknownVal();
1270 }
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Ted Kremenek25c54572009-07-20 22:58:02 +00001272 return RetrieveLazySymbol(state, R);
1273}
1274
Ted Kremenek9031dd72009-07-21 00:12:07 +00001275SVal RegionStoreManager::RetrieveVar(const GRState *state,
1276 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Ted Kremenek9031dd72009-07-21 00:12:07 +00001278 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001279 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Zhongxing Xu13d50172009-10-11 08:08:02 +00001281 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001282 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Ted Kremenek9031dd72009-07-21 00:12:07 +00001284 // Lazily derive a value for the VarRegion.
1285 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Ted Kremenek9031dd72009-07-21 00:12:07 +00001287 if (R->hasGlobalsOrParametersStorage())
1288 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001289
Ted Kremenek9031dd72009-07-21 00:12:07 +00001290 return UndefinedVal();
1291}
1292
Mike Stump1eb44332009-09-09 15:08:12 +00001293SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001294 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Ted Kremenek25c54572009-07-20 22:58:02 +00001296 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001297
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001298 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001299 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001300}
1301
Mike Stump1eb44332009-09-09 15:08:12 +00001302SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1303 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001304 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001305 assert(T->isStructureType());
1306
Zhongxing Xub7507d12009-06-11 07:27:30 +00001307 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001308 RecordDecl* RD = RT->getDecl();
1309 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001310 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001311#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001312 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1313
Ted Kremenek67f28532009-06-17 22:02:04 +00001314 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1315 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001316 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001317
Douglas Gregore267ff32008-12-11 20:41:00 +00001318 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1319 FieldEnd = Fields.rend();
1320 Field != FieldEnd; ++Field) {
1321 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001322 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001323 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001324 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1325 }
1326
Zhongxing Xud91ee272009-06-23 09:02:15 +00001327 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001328#else
1329 return ValMgr.makeLazyCompoundVal(state, R);
1330#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001331}
1332
Ted Kremenek67f28532009-06-17 22:02:04 +00001333SVal RegionStoreManager::RetrieveArray(const GRState *state,
1334 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001335#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001336 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001337 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1338
1339 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001340 uint64_t size = CAT->getSize().getZExtValue();
1341 for (uint64_t i = 0; i < size; ++i) {
1342 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001343 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001344 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001345 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001346 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001347 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1348 }
1349
Zhongxing Xud91ee272009-06-23 09:02:15 +00001350 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001351#else
1352 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1353 return ValMgr.makeLazyCompoundVal(state, R);
1354#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001355}
1356
Ted Kremenek9af46f52009-06-16 22:36:44 +00001357//===----------------------------------------------------------------------===//
1358// Binding values to regions.
1359//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001360
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001361Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001362 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Ted Kremenek0964a062009-01-21 06:57:53 +00001364 if (isa<loc::MemRegionVal>(L))
1365 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Ted Kremenek0964a062009-01-21 06:57:53 +00001367 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001368 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001369 return RBFactory.Remove(B, R).getRoot();
1370 }
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenek0964a062009-01-21 06:57:53 +00001372 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001373}
1374
Ted Kremenek67f28532009-06-17 22:02:04 +00001375const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001376 if (isa<loc::ConcreteInt>(L))
1377 return state;
1378
Ted Kremenek9af46f52009-06-16 22:36:44 +00001379 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001380 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Ted Kremenek9af46f52009-06-16 22:36:44 +00001382 // Check if the region is a struct region.
1383 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1384 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001385 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001387 // Special case: the current region represents a cast and it and the super
1388 // region both have pointer types or intptr_t types. If so, perform the
1389 // bind to the super region.
1390 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001391 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001392 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1393 if (ER->getIndex().isZeroConstant()) {
1394 if (const TypedRegion *superR =
1395 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1396 ASTContext &Ctx = getContext();
1397 QualType superTy = superR->getValueType(Ctx);
1398 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001399
1400 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001401 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001402 SValuator::CastResult cr =
1403 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001404 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1405 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001406 // For now, just invalidate the fields of the struct/union/class.
1407 // FIXME: Precisely handle the fields of the record.
1408 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001409 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001410 }
1411 }
1412 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001413 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1414 // Binding directly to a symbolic region should be treated as binding
1415 // to element 0.
1416 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001417 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001418 R = GetElementZeroRegion(SR, T);
1419 }
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001421 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001422 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001423 return state->makeWithStore(
1424 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001425}
1426
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001427const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001428 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001429 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001430
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001431 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001432
Ted Kremenek0964a062009-01-21 06:57:53 +00001433 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001434 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001435 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001436 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001437
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001438 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001439}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001440
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001441// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001442const GRState *
1443RegionStoreManager::BindCompoundLiteral(const GRState *state,
1444 const CompoundLiteralExpr* CL,
1445 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001447 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001448 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001449}
1450
Ted Kremenek027e2662009-11-19 20:20:24 +00001451const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1452 const MemRegion *R,
1453 QualType T) {
1454 Store store = state->getStore();
1455 RegionBindings B = GetRegionBindings(store);
1456 SVal V;
1457
1458 if (Loc::IsLocType(T))
1459 V = ValMgr.makeNull();
1460 else if (T->isIntegerType())
1461 V = ValMgr.makeZeroVal(T);
1462 else if (T->isStructureType() || T->isArrayType()) {
1463 // Set the default value to a zero constant when it is a structure
1464 // or array. The type doesn't really matter.
1465 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1466 }
1467 else {
1468 return state;
1469 }
1470
1471 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1472 return state->makeWithStore(B.getRoot());
1473}
1474
Ted Kremenek67f28532009-06-17 22:02:04 +00001475const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001476 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001477 SVal Init) {
1478
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001479 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001480 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001481 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001482
Ted Kremenek46537392009-07-16 01:33:37 +00001483 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001484
1485 // Check if the init expr is a StringLiteral.
1486 if (isa<loc::MemRegionVal>(Init)) {
1487 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1488 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1489 const char* str = S->getStrData();
1490 unsigned len = S->getByteLength();
1491 unsigned j = 0;
1492
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001493 // Copy bytes from the string literal into the target array. Trailing bytes
1494 // in the array that are not covered by the string literal are initialized
1495 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001496 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001497 if (j >= len)
1498 break;
1499
Ted Kremenek46537392009-07-16 01:33:37 +00001500 SVal Idx = ValMgr.makeArrayIndex(i);
1501 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1502 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001503
Zhongxing Xud91ee272009-06-23 09:02:15 +00001504 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001505 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001506 }
1507
Ted Kremenek67f28532009-06-17 22:02:04 +00001508 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001509 }
1510
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001511 // Handle lazy compound values.
1512 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1513 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
1515 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001516
1517 if (Init.isUnknown())
1518 return setImplicitDefaultValue(state, R, ElementTy);
1519
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001520 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001521 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001522 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Ted Kremenek46537392009-07-16 01:33:37 +00001524 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001525 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001526 if (VI == VE)
1527 break;
1528
Ted Kremenek46537392009-07-16 01:33:37 +00001529 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001530 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001531
1532 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001533 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001534 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001535 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001536 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001537 }
1538
Ted Kremenek027e2662009-11-19 20:20:24 +00001539 // If the init list is shorter than the array length, set the
1540 // array default value.
1541 if (i < size)
1542 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001543
Ted Kremenek67f28532009-06-17 22:02:04 +00001544 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001545}
1546
Ted Kremenek67f28532009-06-17 22:02:04 +00001547const GRState *
1548RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1549 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Ted Kremenek67f28532009-06-17 22:02:04 +00001551 if (!Features.supportsFields())
1552 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001554 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001555 assert(T->isStructureType());
1556
Ted Kremenek6217b802009-07-29 21:53:49 +00001557 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001558 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001559
1560 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001561 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001562
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001563 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001564 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001565 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Ted Kremenek67f28532009-06-17 22:02:04 +00001567 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1568 // Ignore them and kill the field values.
1569 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001570 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001571
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001572 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001573 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001574
1575 RecordDecl::field_iterator FI, FE;
1576
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001577 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001578
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001579 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001580 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001581
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001582 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001583 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001584
Ted Kremenekcf549592009-09-22 21:19:14 +00001585 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001586 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001587 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001588 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001589 else
1590 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001591 }
1592
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001593 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001594 if (FI != FE) {
1595 Store store = state->getStore();
1596 RegionBindings B = GetRegionBindings(store);
1597 B = RBFactory.Add(B, R,
1598 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1599 state = state->makeWithStore(B.getRoot());
1600 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001601
Ted Kremenek67f28532009-06-17 22:02:04 +00001602 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001603}
1604
Zhongxing Xu13d50172009-10-11 08:08:02 +00001605Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1606 RegionBindings B = GetRegionBindings(store);
1607 llvm::OwningPtr<RegionStoreSubRegionMap>
1608 SubRegions(getRegionStoreSubRegionMap(store));
1609 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001610
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001611 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001612 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001613
Zhongxing Xu13d50172009-10-11 08:08:02 +00001614 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001615}
1616
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001617const GRState*
1618RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1619 const GRState *state,
1620 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001621
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001622 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001623 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001624
Mike Stump1eb44332009-09-09 15:08:12 +00001625 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001626 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001627
Mike Stump1eb44332009-09-09 15:08:12 +00001628 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001629 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001631 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1632 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001633 return state->makeWithStore(
1634 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001635}
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Ted Kremenek9af46f52009-06-16 22:36:44 +00001637//===----------------------------------------------------------------------===//
1638// State pruning.
1639//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001640
Mike Stump1eb44332009-09-09 15:08:12 +00001641void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001642 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001643 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001644{
Ted Kremenek781115c2009-10-17 17:45:11 +00001645 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1646
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001647 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001648 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Ted Kremenek9af46f52009-06-16 22:36:44 +00001650 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001651 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001652 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001653
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001654 // Do a pass over the regions in the store. For VarRegions we check if
1655 // the variable is still live and if so add it to the list of live roots.
1656 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001657 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001658
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001659 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001660 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001661 const MemRegion *R = I.getKey();
1662 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001663 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001664
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001665 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001666 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001667 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001668 llvm::SmallVector<RBDNode, 10> Postponed;
1669
Zhongxing Xu6800b332009-10-18 04:15:47 +00001670 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001671
Ted Kremenek9af46f52009-06-16 22:36:44 +00001672 while (!IntermediateRoots.empty()) {
1673 const MemRegion* R = IntermediateRoots.back();
1674 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001675
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001676 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001677 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001678 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001679
Ted Kremenek9af46f52009-06-16 22:36:44 +00001680 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001681 if (SymReaper.isLive(Loc, VR->getDecl()))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001682 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001683 continue;
1684 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001685
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001686 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001687 llvm::SmallVectorImpl<RBDNode> &Q =
1688 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1689
1690 Q.push_back(std::make_pair(&state, SR));
1691
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001692 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001693 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001694
1695 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001696 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001697 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001698 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001699 }
Mike Stump1eb44332009-09-09 15:08:12 +00001700
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001701 // Enqueue the RegionRoots onto WorkList.
1702 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1703 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001704 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001705 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001706 RegionRoots.clear();
1707
Zhongxing Xu6800b332009-10-18 04:15:47 +00001708 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001709
Ted Kremenek01756192009-10-29 05:14:17 +00001710tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001711 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001712 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001713 WorkList.pop_back();
1714
1715 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001716 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001717 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001718 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001720 const MemRegion *R = N.second;
1721 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001722
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001723 // Enqueue subregions.
1724 RegionStoreSubRegionMap *M;
1725
1726 if (&state == state_N)
1727 M = SubRegions.get();
1728 else {
1729 RegionStoreSubRegionMap *& SM = SC[state_N];
1730 if (!SM)
1731 SM = getRegionStoreSubRegionMap(state_N->getStore());
1732 M = SM;
1733 }
1734
1735 RegionStoreSubRegionMap::iterator I, E;
1736 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1737 WorkList.push_back(std::make_pair(state_N, *I));
1738
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001739 // Enqueue the super region.
1740 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1741 const MemRegion *superR = SR->getSuperRegion();
1742 if (!isa<MemSpaceRegion>(superR)) {
1743 // If 'R' is a field or an element, we want to keep the bindings
1744 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001745 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001746 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1747 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001748 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001749 }
1750 }
1751
1752 // Mark the symbol for any live SymbolicRegion as "live". This means we
1753 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001754 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001755 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001756
1757 // For BlockDataRegions, enqueue all VarRegions for that are referenced
1758 // via BlockDeclRefExprs.
1759 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1760 for (BlockDataRegion::referenced_vars_iterator
1761 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1762 RI != RE; ++RI)
1763 WorkList.push_back(std::make_pair(state_N, *RI));
1764
1765 // No possible data bindings on a BlockDataRegion. Continue to the
1766 // next region in the worklist.
1767 continue;
1768 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001769
1770 Store store_N = state_N->getStore();
1771 RegionBindings B_N = GetRegionBindings(store_N);
1772
1773 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001774 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001775
Zhongxing Xu13d50172009-10-11 08:08:02 +00001776 if (V) {
1777 // Check for lazy bindings.
1778 if (const nonloc::LazyCompoundVal *LCV =
1779 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001780
Zhongxing Xu13d50172009-10-11 08:08:02 +00001781 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001782 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001783 }
1784 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001785 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001786 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001787 SI!=SE;++SI)
1788 SymReaper.markLive(*SI);
1789
Zhongxing Xu13d50172009-10-11 08:08:02 +00001790 // If V is a region, then add it to the worklist.
1791 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001792 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001793 }
1794 }
1795 }
1796
Ted Kremenek01756192009-10-29 05:14:17 +00001797 // See if any postponed SymbolicRegions are actually live now, after
1798 // having done a scan.
1799 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1800 E = Postponed.end() ; I != E ; ++I) {
1801 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1802 if (SymReaper.isLive(SR->getSymbol())) {
1803 WorkList.push_back(*I);
1804 I->second = NULL;
1805 }
1806 }
1807 }
1808
1809 if (!WorkList.empty())
1810 goto tryAgain;
1811
Ted Kremenek9af46f52009-06-16 22:36:44 +00001812 // We have now scanned the store, marking reachable regions and symbols
1813 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001814 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001815 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001816 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001817 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001818 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001819 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Ted Kremenek9af46f52009-06-16 22:36:44 +00001821 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001822 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Ted Kremenek9af46f52009-06-16 22:36:44 +00001824 // Mark all non-live symbols that this region references as dead.
1825 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1826 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Zhongxing Xu13d50172009-10-11 08:08:02 +00001828 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001829 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1830 for (; SI != SE; ++SI)
1831 SymReaper.maybeDead(*SI);
1832 }
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001834 // Write the store back.
1835 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001836}
1837
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001838GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1839 StackFrameContext const *frame) {
1840 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1841 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1842
1843 FunctionDecl::param_const_iterator PI = FD->param_begin();
1844
1845 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1846
1847 // Copy the arg expression value to the arg variables.
1848 for (; AI != AE; ++AI, ++PI) {
1849 SVal ArgVal = state->getSVal(*AI);
1850 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1851 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1852 }
1853
1854 return state;
1855}
1856
Ted Kremenek9af46f52009-06-16 22:36:44 +00001857//===----------------------------------------------------------------------===//
1858// Utility methods.
1859//===----------------------------------------------------------------------===//
1860
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001861void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001862 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001863 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001864 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Ted Kremenek451ac092009-08-06 04:50:20 +00001866 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001867 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001868}