blob: e645172a5b66e69f2f5d695a8bdfb2accd41b511 [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
29using namespace clang;
30
Ted Kremenek356e9d62009-07-22 04:35:42 +000031#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000033
Zhongxing Xu13d50172009-10-11 08:08:02 +000034namespace {
35class BindingVal {
36public:
37 enum BindingKind { Direct, Default };
38private:
39 SVal Value;
40 BindingKind Kind;
41
42public:
43 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
44
45 bool isDefault() const { return Kind == Default; }
46
47 const SVal *getValue() const { return &Value; }
48
49 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
50
51 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
52
53 void Profile(llvm::FoldingSetNodeID& ID) const {
54 Value.Profile(ID);
55 ID.AddInteger(Kind);
56 }
57
58 inline bool operator==(const BindingVal& R) const {
59 return Value == R.Value && Kind == R.Kind;
60 }
61
62 inline bool operator!=(const BindingVal& R) const {
63 return !(*this == R);
64 }
65};
66}
67
68namespace llvm {
69static inline
70llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
71 if (V.isDefault())
72 os << "(default) ";
73 else
74 os << "(direct) ";
75 os << *V.getValue();
76 return os;
77}
78} // end llvm namespace
79
Zhongxing Xubaf03a72008-11-24 09:44:56 +000080// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000081typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000082
Ted Kremenek50dc1b32008-12-24 01:05:03 +000083//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000084// Fine-grained control of RegionStoreManager.
85//===----------------------------------------------------------------------===//
86
87namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000088struct minimal_features_tag {};
89struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000090
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000091class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +000092 bool SupportsFields;
93 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Ted Kremenek9af46f52009-06-16 22:36:44 +000095public:
96 RegionStoreFeatures(minimal_features_tag) :
97 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek9af46f52009-06-16 22:36:44 +000099 RegionStoreFeatures(maximal_features_tag) :
100 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek9af46f52009-06-16 22:36:44 +0000102 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek9af46f52009-06-16 22:36:44 +0000104 bool supportsFields() const { return SupportsFields; }
105 bool supportsRemaining() const { return SupportsRemaining; }
106};
107}
108
109//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110// Region "Extents"
111//===----------------------------------------------------------------------===//
112//
113// MemRegions represent chunks of memory with a size (their "extent"). This
114// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000115//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000116namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000118namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000119 template<> struct GRStateTrait<RegionExtents>
120 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
121 static void* GDMIndex() { return &RegionExtentsIndex; }
122 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000123}
124
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000125//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000126// Utility functions.
127//===----------------------------------------------------------------------===//
128
129static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
130 if (ty->isAnyPointerType())
131 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000133 return ty->isIntegerType() && ty->isScalarType() &&
134 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
135}
136
137//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000138// Main RegionStore logic.
139//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000140
Zhongxing Xu17892752008-10-08 02:50:44 +0000141namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000143class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000145 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000146 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000149 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000150 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000151
152 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000153 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000154 return true;
155 }
156
157 I->second = F.Add(I->second, SubRegion);
158 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000161 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek5dc27462009-03-03 02:51:43 +0000165 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000166 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167
168 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000169 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek59e8f112009-03-03 01:35:36 +0000171 llvm::ImmutableSet<const MemRegion*> S = I->second;
172 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
173 SI != SE; ++SI) {
174 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000175 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek5dc27462009-03-03 02:51:43 +0000178 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000181 typedef SetTy::iterator iterator;
182
183 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
184 Map::iterator I = M.find(R);
185 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
186 return std::make_pair(S.begin(), S.end());
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000189
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000190class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000191 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000192 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000193
194 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
195 SMCache SC;
196
Zhongxing Xu17892752008-10-08 02:50:44 +0000197public:
Mike Stump1eb44332009-09-09 15:08:12 +0000198 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000199 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000200 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000201 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000202
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000203 virtual ~RegionStoreManager() {
204 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
205 delete (*I).second;
206 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000207
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000208 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Zhongxing Xu13d50172009-10-11 08:08:02 +0000210 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Zhongxing Xu13d50172009-10-11 08:08:02 +0000212 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
213 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000214 /// getDefaultBinding - Returns an SVal* representing an optional default
215 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000216 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000217
218 /// setImplicitDefaultValue - Set the default binding for the provided
219 /// MemRegion to the value implicitly defined for compound literals when
220 /// the value is not specified.
221 const GRState *setImplicitDefaultValue(const GRState *state,
222 const MemRegion *R,
223 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000225 /// getLValueString - Returns an SVal representing the lvalue of a
226 /// StringLiteral. Within RegionStore a StringLiteral has an
227 /// associated StringRegion, and the lvalue of a StringLiteral is
228 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000229 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000230
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000231 /// getLValueCompoundLiteral - Returns an SVal representing the
232 /// lvalue of a compound literal. Within RegionStore a compound
233 /// literal has an associated region, and the lvalue of the
234 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000235 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000236
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000237 /// getLValueVar - Returns an SVal that represents the lvalue of a
238 /// variable. Within RegionStore a variable has an associated
239 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000240 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000242 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000243
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000244 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000246 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000247
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000248 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000249
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000250
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000251 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
252 /// type. 'Array' represents the lvalue of the array being decayed
253 /// to a pointer, and the returned SVal represents the decayed
254 /// version of that lvalue (i.e., a pointer to the first element of
255 /// the array). This is called by GRExprEngine when evaluating
256 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000257 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000258
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000259 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000260 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000261
Mike Stump1eb44332009-09-09 15:08:12 +0000262 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000263 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000264 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 //===-------------------------------------------------------------------===//
267 // Binding values to regions.
268 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000269
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000270 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000271 const Expr *E, unsigned Count,
272 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000274private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000275 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000276 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000277
278public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000279 const GRState *Bind(const GRState *state, Loc LV, SVal V);
280
281 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000282 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000284 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
285 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000286
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000287 const GRState *BindDeclWithNoInit(const GRState *state,
288 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000289 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000290 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000291
Ted Kremenek67f28532009-06-17 22:02:04 +0000292 /// BindStruct - Bind a compound value to a structure.
293 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremenek67f28532009-06-17 22:02:04 +0000295 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000296
297 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000298 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 Store Remove(Store store, Loc LV);
301
302 //===------------------------------------------------------------------===//
303 // Loading values from regions.
304 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000305
Ted Kremenek67f28532009-06-17 22:02:04 +0000306 /// The high level logic for this method is this:
307 /// Retrieve (L)
308 /// if L has binding
309 /// return L's binding
310 /// else if L is in killset
311 /// return unknown
312 /// else
313 /// if L is on stack or heap
314 /// return undefined
315 /// else
316 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000317 SValuator::CastResult Retrieve(const GRState *state, Loc L,
318 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000319
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000320 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000321
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000322 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000324 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Ted Kremenek9031dd72009-07-21 00:12:07 +0000326 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Ted Kremenek25c54572009-07-20 22:58:02 +0000328 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000329
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000330 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
331 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek67f28532009-06-17 22:02:04 +0000333 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000334 /// struct copy:
335 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000336 /// x = y;
337 /// y's value is retrieved by this method.
338 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek67f28532009-06-17 22:02:04 +0000340 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000342 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000343 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000345 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
346 const GRState *state,
347 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000348
Ted Kremenek0954cde2009-09-24 04:11:44 +0000349 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
350 QualType T);
351
Ted Kremenek67f28532009-06-17 22:02:04 +0000352 //===------------------------------------------------------------------===//
353 // State pruning.
354 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Ted Kremenek67f28532009-06-17 22:02:04 +0000356 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
357 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000358 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000359 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
360
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000361 const GRState *EnterStackFrame(const GRState *state,
362 const StackFrameContext *frame);
363
Ted Kremenek67f28532009-06-17 22:02:04 +0000364 //===------------------------------------------------------------------===//
365 // Region "extents".
366 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Ted Kremenek67f28532009-06-17 22:02:04 +0000368 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000369 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
370 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000371
372 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000373 // Utility methods.
374 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek451ac092009-08-06 04:50:20 +0000376 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000377 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000378 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000379
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000380 void print(Store store, llvm::raw_ostream& Out, const char* nl,
381 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000382
383 void iterBindings(Store store, BindingsHandler& f) {
384 // FIXME: Implement.
385 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000386
Ted Kremenek67f28532009-06-17 22:02:04 +0000387 // FIXME: Remove.
388 BasicValueFactory& getBasicVals() {
389 return StateMgr.getBasicVals();
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Ted Kremenek67f28532009-06-17 22:02:04 +0000392 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000393 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000394};
395
396} // end anonymous namespace
397
Ted Kremenek9af46f52009-06-16 22:36:44 +0000398//===----------------------------------------------------------------------===//
399// RegionStore creation.
400//===----------------------------------------------------------------------===//
401
402StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
403 RegionStoreFeatures F = maximal_features_tag();
404 return new RegionStoreManager(StMgr, F);
405}
406
407StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
408 RegionStoreFeatures F = minimal_features_tag();
409 F.enableFields(true);
410 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000411}
412
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000413void
414RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000415 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000416 const MemRegion *superR = R->getSuperRegion();
417 if (add(superR, R))
418 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000419 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000420}
421
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000422RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000423RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
424 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000425 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000427 llvm::SmallVector<const SubRegion*, 10> WL;
428
Ted Kremenek451ac092009-08-06 04:50:20 +0000429 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
431 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Mike Stump1eb44332009-09-09 15:08:12 +0000433 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000434 // don't have direct bindings but are super regions of those that do.
435 while (!WL.empty()) {
436 const SubRegion *R = WL.back();
437 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000438 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000439 }
440
Ted Kremenek14453bf2009-03-03 19:02:42 +0000441 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000442}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000443
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000445 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000446}
447
Ted Kremenek9af46f52009-06-16 22:36:44 +0000448//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000449// Binding invalidation.
450//===----------------------------------------------------------------------===//
451
Zhongxing Xu13d50172009-10-11 08:08:02 +0000452void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
453 const MemRegion *R,
454 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000455 RegionStoreSubRegionMap::iterator I, E;
456
457 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000458 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000460 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000461}
462
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000463const GRState *RegionStoreManager::InvalidateRegion(const GRState *state,
464 const MemRegion *R,
Ted Kremenek87806792009-09-27 20:45:21 +0000465 const Expr *Ex,
Ted Kremenek473e1672009-10-16 00:30:49 +0000466 unsigned Count,
467 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000468 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000470 // Strip away casts.
Zhongxing Xu479529e2009-11-10 02:17:20 +0000471 R = R->StripCasts();
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000472
Ted Kremenek87806792009-09-27 20:45:21 +0000473 // Get the mapping of regions -> subregions.
474 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000475 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000476
477 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000478
Ted Kremenek87806792009-09-27 20:45:21 +0000479 llvm::DenseMap<const MemRegion *, unsigned> Visited;
480 llvm::SmallVector<const MemRegion *, 10> WorkList;
481 WorkList.push_back(R);
482
483 while (!WorkList.empty()) {
484 R = WorkList.back();
485 WorkList.pop_back();
486
487 // Have we visited this region before?
488 unsigned &visited = Visited[R];
489 if (visited)
490 continue;
491 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Ted Kremenek87806792009-09-27 20:45:21 +0000493 // Add subregions to work list.
494 RegionStoreSubRegionMap::iterator I, E;
495 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
496 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000497
498 // Get the old binding. Is it a region? If so, add it to the worklist.
499 if (Optional<SVal> V = getDirectBinding(B, R)) {
500 if (const MemRegion *RV = V->getAsRegion())
501 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000502
503 // A symbol? Mark it touched by the invalidation.
504 if (IS) {
505 if (SymbolRef Sym = V->getAsSymbol())
506 IS->insert(Sym);
507 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000508 }
509
Ted Kremenek473e1672009-10-16 00:30:49 +0000510 // Symbolic region? Mark that symbol touched by the invalidation.
511 if (IS) {
512 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
513 IS->insert(SR->getSymbol());
514 }
515
516 // Handle the region itself.
Ted Kremenek87806792009-09-27 20:45:21 +0000517 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
518 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000519 // Invalidate the region by setting its default value to
520 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000521 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
522 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000523 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000524 continue;
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Ted Kremenek87806792009-09-27 20:45:21 +0000527 if (!R->isBoundable())
528 continue;
529
530 const TypedRegion *TR = cast<TypedRegion>(R);
531 QualType T = TR->getValueType(Ctx);
532
533 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000534 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
535
Zhongxing Xu13d50172009-10-11 08:08:02 +0000536 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000537 if (!RD)
538 continue;
539
Zhongxing Xu13d50172009-10-11 08:08:02 +0000540 // Invalidate the region by setting its default value to
541 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000542 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
543 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000544 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000545 continue;
546 }
547
548 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
549 // Set the default value of the array to conjured symbol.
550 DefinedOrUnknownSVal V =
551 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000552 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000553 continue;
554 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000555
556 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
557 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000558 // For fields and elements whose super region has also been invalidated,
559 // only remove the old binding. The super region will get set with a
560 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000561 B = RBFactory.Remove(B, R);
562 continue;
563 }
Ted Kremenek87806792009-09-27 20:45:21 +0000564
Ted Kremenek389c44c2009-09-29 03:12:50 +0000565 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000566 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
567 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000568 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000569 }
570
Ted Kremenek87806792009-09-27 20:45:21 +0000571 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000572 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000573}
574
575//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000576// getLValueXXX methods.
577//===----------------------------------------------------------------------===//
578
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000579/// getLValueString - Returns an SVal representing the lvalue of a
580/// StringLiteral. Within RegionStore a StringLiteral has an
581/// associated StringRegion, and the lvalue of a StringLiteral is the
582/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000583SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000584 return loc::MemRegionVal(MRMgr.getStringRegion(S));
585}
586
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000587/// getLValueVar - Returns an SVal that represents the lvalue of a
588/// variable. Within RegionStore a variable has an associated
589/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000590SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000591 const LocationContext *LC) {
592 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000593}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000594
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000595/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
596/// of a compound literal. Within RegionStore a compound literal
597/// has an associated region, and the lvalue of the compound literal
598/// is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000599SVal
600RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000601 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
602}
603
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000604SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
605 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000606}
607
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000608SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
609 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000610}
611
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000612SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000613 if (Base.isUnknownOrUndef())
614 return Base;
615
616 Loc BaseL = cast<Loc>(Base);
617 const MemRegion* BaseR = 0;
618
619 switch (BaseL.getSubKind()) {
620 case loc::MemRegionKind:
621 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
622 break;
623
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000624 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000625 // These are anormal cases. Flag an undefined value.
626 return UndefinedVal();
627
628 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000629 // While these seem funny, this can happen through casts.
630 // FIXME: What we should return is the field offset. For example,
631 // add the field offset to the integer value. That way funny things
632 // like this work properly: &(((struct foo *) 0xa)->f)
633 return Base;
634
635 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000636 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000637 return Base;
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000640 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
641 // of FieldDecl.
642 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
643 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000644
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000645 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000646}
647
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000648SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
649 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000650
Ted Kremenekde7ec632009-03-09 22:44:49 +0000651 // If the base is an unknown or undefined value, just return it back.
652 // FIXME: For absolute pointer addresses, we just return that value back as
653 // well, although in reality we should return the offset added to that
654 // value.
655 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000656 return Base;
657
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000658 // Only handle integer offsets... for now.
659 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000660 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000661
Zhongxing Xuce760782009-05-09 13:20:07 +0000662 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000663
664 // Pointer of any type can be cast and used as array base.
665 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Ted Kremenek46537392009-07-16 01:33:37 +0000667 // Convert the offset to the appropriate size and signedness.
668 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000670 if (!ElemR) {
671 //
672 // If the base region is not an ElementRegion, create one.
673 // This can happen in the following example:
674 //
675 // char *p = __builtin_alloc(10);
676 // p[1] = 8;
677 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000678 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000679 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000680 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000681 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000682 }
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000684 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000686 if (!isa<nonloc::ConcreteInt>(BaseIdx))
687 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000689 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
690 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
691 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremenek46537392009-07-16 01:33:37 +0000693 // Compute the new index.
694 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Ted Kremenek46537392009-07-16 01:33:37 +0000696 // Construct the new ElementRegion.
697 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000698 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000699 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000700}
701
Ted Kremenek9af46f52009-06-16 22:36:44 +0000702//===----------------------------------------------------------------------===//
703// Extents for regions.
704//===----------------------------------------------------------------------===//
705
Zhongxing Xue884ff82009-11-12 02:48:32 +0000706DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
707 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000709 switch (R->getKind()) {
710 case MemRegion::MemSpaceRegionKind:
711 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000712 return UnknownVal();
713
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000714 case MemRegion::FunctionTextRegionKind:
715 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000716 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000717 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000718 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000719
720 // Not yet handled.
721 case MemRegion::AllocaRegionKind:
722 case MemRegion::CompoundLiteralRegionKind:
723 case MemRegion::ElementRegionKind:
724 case MemRegion::FieldRegionKind:
725 case MemRegion::ObjCIvarRegionKind:
726 case MemRegion::ObjCObjectRegionKind:
727 case MemRegion::SymbolicRegionKind:
728 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000730 case MemRegion::StringRegionKind: {
731 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000732 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000733 // operations with signed indices.
734 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000735 }
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000737 case MemRegion::VarRegionKind: {
738 const VarRegion* VR = cast<VarRegion>(R);
739 // Get the type of the variable.
740 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000742 // FIXME: Handle variable-length arrays.
743 if (isa<VariableArrayType>(T))
744 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000746 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
747 // return the size as signed integer.
748 return ValMgr.makeIntVal(CAT->getSize(), false);
749 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000750
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000751 // Clients can use ordinary variables as if they were arrays. These
752 // essentially are arrays of size 1.
753 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000756 case MemRegion::BEG_DECL_REGIONS:
757 case MemRegion::END_DECL_REGIONS:
758 case MemRegion::BEG_TYPED_REGIONS:
759 case MemRegion::END_TYPED_REGIONS:
760 assert(0 && "Infeasible region");
761 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000762 }
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000764 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000765 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000766}
767
Ted Kremenek67f28532009-06-17 22:02:04 +0000768const GRState *RegionStoreManager::setExtent(const GRState *state,
769 const MemRegion *region,
770 SVal extent) {
771 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000772}
773
774//===----------------------------------------------------------------------===//
775// Location and region casting.
776//===----------------------------------------------------------------------===//
777
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000778/// ArrayToPointer - Emulates the "decay" of an array to a pointer
779/// type. 'Array' represents the lvalue of the array being decayed
780/// to a pointer, and the returned SVal represents the decayed
781/// version of that lvalue (i.e., a pointer to the first element of
782/// the array). This is called by GRExprEngine when evaluating casts
783/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000784SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000785 if (!isa<loc::MemRegionVal>(Array))
786 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Ted Kremenekabb042f2008-12-13 19:24:37 +0000788 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
789 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000791 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000792 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000794 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000795 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000796 ArrayType *AT = cast<ArrayType>(T);
797 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Ted Kremenek75185b52009-07-16 00:00:11 +0000799 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
800 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000801
802 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000803}
804
Ted Kremenek9af46f52009-06-16 22:36:44 +0000805//===----------------------------------------------------------------------===//
806// Pointer arithmetic.
807//===----------------------------------------------------------------------===//
808
Mike Stump1eb44332009-09-09 15:08:12 +0000809SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000810 BinaryOperator::Opcode Op, Loc L, NonLoc R,
811 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000812 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000813 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000814 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000815
Zhongxing Xua1718c72009-04-03 07:33:13 +0000816 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000817 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000818
Ted Kremenek3bccf082009-07-11 00:58:27 +0000819 switch (MR->getKind()) {
820 case MemRegion::SymbolicRegionKind: {
821 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000822 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000823 QualType T = Sym->getType(getContext());
824 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000826 if (const PointerType *PT = T->getAs<PointerType>())
827 EleTy = PT->getPointeeType();
828 else
John McCall183700f2009-09-21 23:43:11 +0000829 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Ted Kremenek3bccf082009-07-11 00:58:27 +0000831 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
832 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000833 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000834 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000835 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000836 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000837 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000838 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000839 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
840 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000841 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000842 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000843
Ted Kremenek3bccf082009-07-11 00:58:27 +0000844 case MemRegion::ElementRegionKind: {
845 ER = cast<ElementRegion>(MR);
846 break;
847 }
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 // Not yet handled.
850 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000851 case MemRegion::StringRegionKind: {
852
853 }
854 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000855 case MemRegion::CompoundLiteralRegionKind:
856 case MemRegion::FieldRegionKind:
857 case MemRegion::ObjCObjectRegionKind:
858 case MemRegion::ObjCIvarRegionKind:
859 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000861 case MemRegion::FunctionTextRegionKind:
862 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000863 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000864 // Technically this can happen if people do funny things with casts.
865 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Ted Kremenek3bccf082009-07-11 00:58:27 +0000867 case MemRegion::MemSpaceRegionKind:
868 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
869 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek3bccf082009-07-11 00:58:27 +0000871 case MemRegion::BEG_DECL_REGIONS:
872 case MemRegion::END_DECL_REGIONS:
873 case MemRegion::BEG_TYPED_REGIONS:
874 case MemRegion::END_TYPED_REGIONS:
875 assert(0 && "Infeasible region");
876 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000877 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000878
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000879 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000880 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000881
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000882 // For now, only support:
883 // (a) concrete integer indices that can easily be resolved
884 // (b) 0 + symbolic index
885 if (Base) {
886 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
887 // FIXME: Should use SValuator here.
888 SVal NewIdx =
889 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000890 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000891 const MemRegion* NewER =
892 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
893 ER->getSuperRegion(), getContext());
894 return ValMgr.makeLoc(NewER);
895 }
896 if (0 == Base->getValue()) {
897 const MemRegion* NewER =
898 MRMgr.getElementRegion(ER->getElementType(), R,
899 ER->getSuperRegion(), getContext());
900 return ValMgr.makeLoc(NewER);
901 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Ted Kremenek5dc27462009-03-03 02:51:43 +0000904 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000905}
906
Ted Kremenek9af46f52009-06-16 22:36:44 +0000907//===----------------------------------------------------------------------===//
908// Loading values from regions.
909//===----------------------------------------------------------------------===//
910
Zhongxing Xu13d50172009-10-11 08:08:02 +0000911Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
912 const MemRegion *R) {
913 if (const BindingVal *BV = B.lookup(R))
914 return Optional<SVal>::create(BV->getDirectValue());
915
916 return Optional<SVal>();
917}
918
919Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000920 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000922 if (R->isBoundable())
923 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
924 if (TR->getValueType(getContext())->isUnionType())
925 return UnknownVal();
926
Zhongxing Xu13d50172009-10-11 08:08:02 +0000927 if (BindingVal const *V = B.lookup(R))
928 return Optional<SVal>::create(V->getDefaultValue());
929
930 return Optional<SVal>();
931}
932
933Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
934 const MemRegion *R) {
935 if (const BindingVal *BV = B.lookup(R))
936 return Optional<SVal>::create(BV->getValue());
937
938 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000939}
940
Ted Kremeneka6275a52009-07-15 02:31:43 +0000941static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
942 RTy = Ctx.getCanonicalType(RTy);
943 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Ted Kremeneka6275a52009-07-15 02:31:43 +0000945 if (RTy == UsedTy)
946 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000947
948
Ted Kremenek25c54572009-07-20 22:58:02 +0000949 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000950 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000951 // represents a reinterpretation.
952 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000953 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000954 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000955
956 return PUsedTy && PRTy &&
957 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000958 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000959 }
960
961 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000962}
963
Ted Kremenek0954cde2009-09-24 04:11:44 +0000964const ElementRegion *
965RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
966 ASTContext &Ctx = getContext();
967 SVal idx = ValMgr.makeZeroArrayIndex();
968 assert(!T.isNull());
969 return MRMgr.getElementRegion(T, idx, SR, Ctx);
970}
971
972
973
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000974SValuator::CastResult
975RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000976
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000977 assert(!isa<UnknownVal>(L) && "location unknown");
978 assert(!isa<UndefinedVal>(L) && "location undefined");
979
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000980 // FIXME: Is this even possible? Shouldn't this be treated as a null
981 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000982 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000983 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000984
Ted Kremenek67f28532009-06-17 22:02:04 +0000985 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000986
Zhongxing Xu91844122009-05-20 09:18:48 +0000987 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000988 // Example:
989 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000990 // char* p = alloca();
991 // read(p);
992 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +0000993 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000994 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Ted Kremenek0954cde2009-09-24 04:11:44 +0000996 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
997 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Ted Kremenek968f0a62009-08-03 21:41:46 +0000999 if (isa<CodeTextRegion>(MR))
1000 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001002 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1003 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001004 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001005 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001006
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001007 // FIXME: We should eventually handle funny addressing. e.g.:
1008 //
1009 // int x = ...;
1010 // int *p = &x;
1011 // char *q = (char*) p;
1012 // char c = *q; // returns the first byte of 'x'.
1013 //
1014 // Such funny addressing will occur due to layering of regions.
1015
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001016#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001017 ASTContext &Ctx = getContext();
1018 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001019 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1020 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001021 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001022 assert(Ctx.getCanonicalType(RTy) ==
1023 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001024 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001025#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001026
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001027 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001028 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001030 // FIXME: Handle unions.
1031 if (RTy->isUnionType())
1032 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001033
1034 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001035 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001036
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001037 // FIXME: handle Vector types.
1038 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001039 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001040
1041 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001042 return SValuator::CastResult(state,
1043 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001044
1045 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001046 return SValuator::CastResult(state,
1047 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Ted Kremenek25c54572009-07-20 22:58:02 +00001049 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001050 return SValuator::CastResult(state,
1051 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Ted Kremenek9031dd72009-07-21 00:12:07 +00001053 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001054 return SValuator::CastResult(state,
1055 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001056
Ted Kremenek451ac092009-08-06 04:50:20 +00001057 RegionBindings B = GetRegionBindings(state->getStore());
1058 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001059
1060 // Check if the region has a binding.
1061 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001062 if (SVal const *SV = V->getValue())
1063 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001064
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001065 // The location does not have a bound value. This means that it has
1066 // the value it had upon its creation and/or entry to the analyzed
1067 // function/method. These are either symbolic values or 'undefined'.
1068
Ted Kremenek356e9d62009-07-22 04:35:42 +00001069#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001070 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001071#else
1072 if (R->hasStackStorage()) {
1073#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001074 // All stack variables are considered to have undefined values
1075 // upon creation. All heap allocated blocks are considered to
1076 // have undefined values as well unless they are explicitly bound
1077 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001078 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001079 }
1080
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001081 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001082 return SValuator::CastResult(state,
1083 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001084}
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001086std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001087RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001088 if (Optional<SVal> OV = getDirectBinding(B, R))
1089 if (const nonloc::LazyCompoundVal *V =
1090 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1091 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001093 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1094 const std::pair<const GRState *, const MemRegion *> &X =
1095 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001097 if (X.first)
1098 return std::make_pair(X.first,
1099 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001100 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001101 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1102 const std::pair<const GRState *, const MemRegion *> &X =
1103 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001105 if (X.first)
1106 return std::make_pair(X.first,
1107 MRMgr.getFieldRegionWithSuper(FR, X.second));
1108 }
1109
1110 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1111}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001112
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001113SVal RegionStoreManager::RetrieveElement(const GRState* state,
1114 const ElementRegion* R) {
1115 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001116 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001117 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001118 return *V;
1119
Ted Kremenek921109a2009-07-01 23:19:52 +00001120 const MemRegion* superR = R->getSuperRegion();
1121
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001122 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001123 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001124 // FIXME: Handle loads from strings where the literal is treated as
1125 // an integer, e.g., *((unsigned int*)"hello")
1126 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001127 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001128 if (T != Ctx.getCanonicalType(R->getElementType()))
1129 return UnknownVal();
1130
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001131 const StringLiteral *Str = StrR->getStringLiteral();
1132 SVal Idx = R->getIndex();
1133 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1134 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001135 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001136 if (i > byteLength) {
1137 // Buffer overflow checking in GRExprEngine should handle this case,
1138 // but we shouldn't rely on it to not overflow here if that checking
1139 // is disabled.
1140 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001141 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001142 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001143 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001144 }
1145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001147 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001148 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001149 if (SymbolRef parentSym = V->getAsSymbol())
1150 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001151
1152 if (V->isUnknownOrUndef())
1153 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001154
1155 // Handle LazyCompoundVals for the immediate super region. Other cases
1156 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001157 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001158 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001160 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1161 return RetrieveElement(LCV->getState(), R);
1162 }
Mike Stump1eb44332009-09-09 15:08:12 +00001163
Ted Kremeneka6275a52009-07-15 02:31:43 +00001164 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001165 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001166 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001167
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001168 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001169}
1170
Mike Stump1eb44332009-09-09 15:08:12 +00001171SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001172 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001173
1174 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001175 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001176 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001177 return *V;
1178
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001179 QualType Ty = R->getValueType(getContext());
1180 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1181}
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001183SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1184 const TypedRegion *R,
1185 QualType Ty,
1186 const MemRegion *superR) {
1187
Mike Stump1eb44332009-09-09 15:08:12 +00001188 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001189 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001191 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001193 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001194 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001195 if (SymbolRef parentSym = D->getAsSymbol())
1196 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001198 if (D->isZeroConstant())
1199 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001201 if (D->isUnknown())
1202 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001204 assert(0 && "Unknown default value");
1205 }
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001207 // If our super region is a field or element itself, walk up the region
1208 // hierarchy to see if there is a default value installed in an ancestor.
1209 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1210 superR = cast<SubRegion>(superR)->getSuperRegion();
1211 continue;
1212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001214 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001217 // Lazy binding?
1218 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001219 const MemRegion *lazyBindingRegion = NULL;
1220 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001222 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001223 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001225 if (isa<ElementRegion>(R))
1226 return RetrieveElement(lazyBindingState,
1227 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001229 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001230 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001231 }
1232
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001233 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001235 if (isa<ElementRegion>(R)) {
1236 // Currently we don't reason specially about Clang-style vectors. Check
1237 // if superR is a vector and if so return Unknown.
1238 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1239 if (typedSuperR->getValueType(getContext())->isVectorType())
1240 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001241 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001244 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001245 }
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001247 // All other values are symbolic.
1248 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001249}
Mike Stump1eb44332009-09-09 15:08:12 +00001250
1251SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001252 const ObjCIvarRegion* R) {
1253
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001254 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001255 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001256
Zhongxing Xu13d50172009-10-11 08:08:02 +00001257 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001258 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001260 const MemRegion *superR = R->getSuperRegion();
1261
Ted Kremenekab22ee92009-10-20 01:20:57 +00001262 // Check if the super region has a default binding.
1263 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001264 if (SymbolRef parentSym = V->getAsSymbol())
1265 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001267 // Other cases: give up.
1268 return UnknownVal();
1269 }
Mike Stump1eb44332009-09-09 15:08:12 +00001270
Ted Kremenek25c54572009-07-20 22:58:02 +00001271 return RetrieveLazySymbol(state, R);
1272}
1273
Ted Kremenek9031dd72009-07-21 00:12:07 +00001274SVal RegionStoreManager::RetrieveVar(const GRState *state,
1275 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Ted Kremenek9031dd72009-07-21 00:12:07 +00001277 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001278 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Zhongxing Xu13d50172009-10-11 08:08:02 +00001280 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001281 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283 // Lazily derive a value for the VarRegion.
1284 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenek9031dd72009-07-21 00:12:07 +00001286 if (R->hasGlobalsOrParametersStorage())
1287 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Ted Kremenek9031dd72009-07-21 00:12:07 +00001289 return UndefinedVal();
1290}
1291
Mike Stump1eb44332009-09-09 15:08:12 +00001292SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001293 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Ted Kremenek25c54572009-07-20 22:58:02 +00001295 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001296
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001297 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001298 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001299}
1300
Mike Stump1eb44332009-09-09 15:08:12 +00001301SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1302 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001303 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001304 assert(T->isStructureType());
1305
Zhongxing Xub7507d12009-06-11 07:27:30 +00001306 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001307 RecordDecl* RD = RT->getDecl();
1308 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001309 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001310#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001311 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1312
Ted Kremenek67f28532009-06-17 22:02:04 +00001313 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1314 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001315 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001316
Douglas Gregore267ff32008-12-11 20:41:00 +00001317 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1318 FieldEnd = Fields.rend();
1319 Field != FieldEnd; ++Field) {
1320 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001321 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001322 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001323 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1324 }
1325
Zhongxing Xud91ee272009-06-23 09:02:15 +00001326 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001327#else
1328 return ValMgr.makeLazyCompoundVal(state, R);
1329#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001330}
1331
Ted Kremenek67f28532009-06-17 22:02:04 +00001332SVal RegionStoreManager::RetrieveArray(const GRState *state,
1333 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001334#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001335 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001336 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1337
1338 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001339 uint64_t size = CAT->getSize().getZExtValue();
1340 for (uint64_t i = 0; i < size; ++i) {
1341 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001342 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001343 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001344 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001345 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001346 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1347 }
1348
Zhongxing Xud91ee272009-06-23 09:02:15 +00001349 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001350#else
1351 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1352 return ValMgr.makeLazyCompoundVal(state, R);
1353#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001354}
1355
Ted Kremenek9af46f52009-06-16 22:36:44 +00001356//===----------------------------------------------------------------------===//
1357// Binding values to regions.
1358//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001359
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001360Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001361 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Ted Kremenek0964a062009-01-21 06:57:53 +00001363 if (isa<loc::MemRegionVal>(L))
1364 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Ted Kremenek0964a062009-01-21 06:57:53 +00001366 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001367 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001368 return RBFactory.Remove(B, R).getRoot();
1369 }
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Ted Kremenek0964a062009-01-21 06:57:53 +00001371 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001372}
1373
Ted Kremenek67f28532009-06-17 22:02:04 +00001374const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001375 if (isa<loc::ConcreteInt>(L))
1376 return state;
1377
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001379 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Ted Kremenek9af46f52009-06-16 22:36:44 +00001381 // Check if the region is a struct region.
1382 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1383 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001384 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001385
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001386 // Special case: the current region represents a cast and it and the super
1387 // region both have pointer types or intptr_t types. If so, perform the
1388 // bind to the super region.
1389 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001390 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001391 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1392 if (ER->getIndex().isZeroConstant()) {
1393 if (const TypedRegion *superR =
1394 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1395 ASTContext &Ctx = getContext();
1396 QualType superTy = superR->getValueType(Ctx);
1397 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001398
1399 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001400 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001401 SValuator::CastResult cr =
1402 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001403 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1404 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001405 // For now, just invalidate the fields of the struct/union/class.
1406 // FIXME: Precisely handle the fields of the record.
1407 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001408 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001409 }
1410 }
1411 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001412 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1413 // Binding directly to a symbolic region should be treated as binding
1414 // to element 0.
1415 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001416 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001417 R = GetElementZeroRegion(SR, T);
1418 }
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001420 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001421 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001422 return state->makeWithStore(
1423 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001424}
1425
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001426const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001427 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001428 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001429
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001430 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001431
Ted Kremenek0964a062009-01-21 06:57:53 +00001432 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001433 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001434 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001435 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001436
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001437 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001438}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001439
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001440// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001441const GRState *
1442RegionStoreManager::BindCompoundLiteral(const GRState *state,
1443 const CompoundLiteralExpr* CL,
1444 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001446 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek67f28532009-06-17 22:02:04 +00001447 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001448}
1449
Ted Kremenek027e2662009-11-19 20:20:24 +00001450const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1451 const MemRegion *R,
1452 QualType T) {
1453 Store store = state->getStore();
1454 RegionBindings B = GetRegionBindings(store);
1455 SVal V;
1456
1457 if (Loc::IsLocType(T))
1458 V = ValMgr.makeNull();
1459 else if (T->isIntegerType())
1460 V = ValMgr.makeZeroVal(T);
1461 else if (T->isStructureType() || T->isArrayType()) {
1462 // Set the default value to a zero constant when it is a structure
1463 // or array. The type doesn't really matter.
1464 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1465 }
1466 else {
1467 return state;
1468 }
1469
1470 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1471 return state->makeWithStore(B.getRoot());
1472}
1473
Ted Kremenek67f28532009-06-17 22:02:04 +00001474const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001475 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001476 SVal Init) {
1477
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001478 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001479 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001480 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001481
Ted Kremenek46537392009-07-16 01:33:37 +00001482 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001483
1484 // Check if the init expr is a StringLiteral.
1485 if (isa<loc::MemRegionVal>(Init)) {
1486 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1487 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1488 const char* str = S->getStrData();
1489 unsigned len = S->getByteLength();
1490 unsigned j = 0;
1491
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001492 // Copy bytes from the string literal into the target array. Trailing bytes
1493 // in the array that are not covered by the string literal are initialized
1494 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001495 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001496 if (j >= len)
1497 break;
1498
Ted Kremenek46537392009-07-16 01:33:37 +00001499 SVal Idx = ValMgr.makeArrayIndex(i);
1500 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1501 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001502
Zhongxing Xud91ee272009-06-23 09:02:15 +00001503 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001504 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001505 }
1506
Ted Kremenek67f28532009-06-17 22:02:04 +00001507 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001508 }
1509
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001510 // Handle lazy compound values.
1511 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1512 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001513
1514 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001515
1516 if (Init.isUnknown())
1517 return setImplicitDefaultValue(state, R, ElementTy);
1518
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001519 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001520 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001521 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Ted Kremenek46537392009-07-16 01:33:37 +00001523 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001524 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001525 if (VI == VE)
1526 break;
1527
Ted Kremenek46537392009-07-16 01:33:37 +00001528 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001529 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001530
1531 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001532 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001533 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001534 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001535 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001536 }
1537
Ted Kremenek027e2662009-11-19 20:20:24 +00001538 // If the init list is shorter than the array length, set the
1539 // array default value.
1540 if (i < size)
1541 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001542
Ted Kremenek67f28532009-06-17 22:02:04 +00001543 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001544}
1545
Ted Kremenek67f28532009-06-17 22:02:04 +00001546const GRState *
1547RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1548 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Ted Kremenek67f28532009-06-17 22:02:04 +00001550 if (!Features.supportsFields())
1551 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001553 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001554 assert(T->isStructureType());
1555
Ted Kremenek6217b802009-07-29 21:53:49 +00001556 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001557 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001558
1559 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001560 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001561
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001562 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001563 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001564 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Ted Kremenek67f28532009-06-17 22:02:04 +00001566 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1567 // Ignore them and kill the field values.
1568 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001569 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001570
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001571 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001572 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001573
1574 RecordDecl::field_iterator FI, FE;
1575
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001576 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001577
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001578 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001579 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001580
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001581 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001582 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001583
Ted Kremenekcf549592009-09-22 21:19:14 +00001584 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001585 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001586 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001587 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001588 else
1589 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001590 }
1591
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001592 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001593 if (FI != FE) {
1594 Store store = state->getStore();
1595 RegionBindings B = GetRegionBindings(store);
1596 B = RBFactory.Add(B, R,
1597 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1598 state = state->makeWithStore(B.getRoot());
1599 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001600
Ted Kremenek67f28532009-06-17 22:02:04 +00001601 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001602}
1603
Zhongxing Xu13d50172009-10-11 08:08:02 +00001604Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1605 RegionBindings B = GetRegionBindings(store);
1606 llvm::OwningPtr<RegionStoreSubRegionMap>
1607 SubRegions(getRegionStoreSubRegionMap(store));
1608 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001609
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001610 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001611 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001612
Zhongxing Xu13d50172009-10-11 08:08:02 +00001613 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001614}
1615
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001616const GRState*
1617RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1618 const GRState *state,
1619 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001620
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001621 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001622 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001623
Mike Stump1eb44332009-09-09 15:08:12 +00001624 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001625 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001626
Mike Stump1eb44332009-09-09 15:08:12 +00001627 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001628 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001630 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1631 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001632 return state->makeWithStore(
1633 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001634}
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Ted Kremenek9af46f52009-06-16 22:36:44 +00001636//===----------------------------------------------------------------------===//
1637// State pruning.
1638//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001639
Mike Stump1eb44332009-09-09 15:08:12 +00001640void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001641 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001642 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001643{
Ted Kremenek781115c2009-10-17 17:45:11 +00001644 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1645
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001646 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001647 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Ted Kremenek9af46f52009-06-16 22:36:44 +00001649 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001650 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001651 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001652
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001653 // Do a pass over the regions in the store. For VarRegions we check if
1654 // the variable is still live and if so add it to the list of live roots.
1655 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001656 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001657
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001658 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001659 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001660 const MemRegion *R = I.getKey();
1661 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001662 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001663
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001664 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001665 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001666 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001667 llvm::SmallVector<RBDNode, 10> Postponed;
1668
Zhongxing Xu6800b332009-10-18 04:15:47 +00001669 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001670
Ted Kremenek9af46f52009-06-16 22:36:44 +00001671 while (!IntermediateRoots.empty()) {
1672 const MemRegion* R = IntermediateRoots.back();
1673 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001674
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001675 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001676 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001677 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001678
Ted Kremenek9af46f52009-06-16 22:36:44 +00001679 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001680 if (SymReaper.isLive(Loc, VR->getDecl()))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001681 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001682 continue;
1683 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001684
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001685 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001686 llvm::SmallVectorImpl<RBDNode> &Q =
1687 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1688
1689 Q.push_back(std::make_pair(&state, SR));
1690
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001691 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001692 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001693
1694 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001695 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001696 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001697 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001698 }
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001700 // Enqueue the RegionRoots onto WorkList.
1701 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1702 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001703 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001704 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001705 RegionRoots.clear();
1706
Zhongxing Xu6800b332009-10-18 04:15:47 +00001707 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001708
Ted Kremenek01756192009-10-29 05:14:17 +00001709tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001710 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001711 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001712 WorkList.pop_back();
1713
1714 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001715 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001716 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001717 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001719 const MemRegion *R = N.second;
1720 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001721
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001722 // Enqueue subregions.
1723 RegionStoreSubRegionMap *M;
1724
1725 if (&state == state_N)
1726 M = SubRegions.get();
1727 else {
1728 RegionStoreSubRegionMap *& SM = SC[state_N];
1729 if (!SM)
1730 SM = getRegionStoreSubRegionMap(state_N->getStore());
1731 M = SM;
1732 }
1733
1734 RegionStoreSubRegionMap::iterator I, E;
1735 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1736 WorkList.push_back(std::make_pair(state_N, *I));
1737
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001738 // Enqueue the super region.
1739 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1740 const MemRegion *superR = SR->getSuperRegion();
1741 if (!isa<MemSpaceRegion>(superR)) {
1742 // If 'R' is a field or an element, we want to keep the bindings
1743 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001744 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001745 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1746 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001747 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001748 }
1749 }
1750
1751 // Mark the symbol for any live SymbolicRegion as "live". This means we
1752 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001753 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001754 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001755
1756 // For BlockDataRegions, enqueue all VarRegions for that are referenced
1757 // via BlockDeclRefExprs.
1758 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1759 for (BlockDataRegion::referenced_vars_iterator
1760 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1761 RI != RE; ++RI)
1762 WorkList.push_back(std::make_pair(state_N, *RI));
1763
1764 // No possible data bindings on a BlockDataRegion. Continue to the
1765 // next region in the worklist.
1766 continue;
1767 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001768
1769 Store store_N = state_N->getStore();
1770 RegionBindings B_N = GetRegionBindings(store_N);
1771
1772 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001773 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001774
Zhongxing Xu13d50172009-10-11 08:08:02 +00001775 if (V) {
1776 // Check for lazy bindings.
1777 if (const nonloc::LazyCompoundVal *LCV =
1778 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001779
Zhongxing Xu13d50172009-10-11 08:08:02 +00001780 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001781 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001782 }
1783 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001784 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001785 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001786 SI!=SE;++SI)
1787 SymReaper.markLive(*SI);
1788
Zhongxing Xu13d50172009-10-11 08:08:02 +00001789 // If V is a region, then add it to the worklist.
1790 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001791 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001792 }
1793 }
1794 }
1795
Ted Kremenek01756192009-10-29 05:14:17 +00001796 // See if any postponed SymbolicRegions are actually live now, after
1797 // having done a scan.
1798 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1799 E = Postponed.end() ; I != E ; ++I) {
1800 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1801 if (SymReaper.isLive(SR->getSymbol())) {
1802 WorkList.push_back(*I);
1803 I->second = NULL;
1804 }
1805 }
1806 }
1807
1808 if (!WorkList.empty())
1809 goto tryAgain;
1810
Ted Kremenek9af46f52009-06-16 22:36:44 +00001811 // We have now scanned the store, marking reachable regions and symbols
1812 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001813 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001814 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001815 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001816 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001817 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001818 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Ted Kremenek9af46f52009-06-16 22:36:44 +00001820 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001821 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Ted Kremenek9af46f52009-06-16 22:36:44 +00001823 // Mark all non-live symbols that this region references as dead.
1824 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1825 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Zhongxing Xu13d50172009-10-11 08:08:02 +00001827 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001828 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1829 for (; SI != SE; ++SI)
1830 SymReaper.maybeDead(*SI);
1831 }
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001833 // Write the store back.
1834 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001835}
1836
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001837GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1838 StackFrameContext const *frame) {
1839 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1840 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1841
1842 FunctionDecl::param_const_iterator PI = FD->param_begin();
1843
1844 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1845
1846 // Copy the arg expression value to the arg variables.
1847 for (; AI != AE; ++AI, ++PI) {
1848 SVal ArgVal = state->getSVal(*AI);
1849 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1850 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1851 }
1852
1853 return state;
1854}
1855
Ted Kremenek9af46f52009-06-16 22:36:44 +00001856//===----------------------------------------------------------------------===//
1857// Utility methods.
1858//===----------------------------------------------------------------------===//
1859
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001860void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001861 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001862 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001863 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Ted Kremenek451ac092009-08-06 04:50:20 +00001865 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001866 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001867}