blob: d825299760f7535dfead3e7d4f90446dc73e7671 [file] [log] [blame]
Zhongxing Xud9959ae2008-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 Xu54f87882009-08-21 13:25:15 +000018#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000019#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xuceca8062008-11-16 04:07:26 +000020#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenek2f6eb142009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xuea8c48d2009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000024
25#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xuceca8062008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Zhongxing Xu1359e002008-10-24 06:01:33 +000027#include "llvm/Support/raw_ostream.h"
Zhongxing Xud9959ae2008-10-08 02:50:44 +000028
29using namespace clang;
30
Ted Kremenek920ad712009-07-22 04:35:42 +000031#define HEAP_UNDEFINED 0
Ted Kremenekfa417142009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek920ad712009-07-22 04:35:42 +000033
Zhongxing Xub8edf2a2009-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 Xu9165ed62008-11-24 09:44:56 +000080// Actual Store type.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +000081typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xu9165ed62008-11-24 09:44:56 +000082
Ted Kremenekae189ec2008-12-24 01:05:03 +000083//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +000084// Fine-grained control of RegionStoreManager.
85//===----------------------------------------------------------------------===//
86
87namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000088struct minimal_features_tag {};
89struct maximal_features_tag {};
Mike Stump11289f42009-09-09 15:08:12 +000090
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000091class RegionStoreFeatures {
Ted Kremenek4533a552009-06-16 22:36:44 +000092 bool SupportsFields;
93 bool SupportsRemaining;
Mike Stump11289f42009-09-09 15:08:12 +000094
Ted Kremenek4533a552009-06-16 22:36:44 +000095public:
96 RegionStoreFeatures(minimal_features_tag) :
97 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +000098
Ted Kremenek4533a552009-06-16 22:36:44 +000099 RegionStoreFeatures(maximal_features_tag) :
100 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump11289f42009-09-09 15:08:12 +0000101
Ted Kremenek4533a552009-06-16 22:36:44 +0000102 void enableFields(bool t) { SupportsFields = t; }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Ted Kremenek4533a552009-06-16 22:36:44 +0000104 bool supportsFields() const { return SupportsFields; }
105 bool supportsRemaining() const { return SupportsRemaining; }
106};
107}
108
109//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-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 Kremenek682c3a62009-01-07 22:18:50 +0000115//
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000116namespace { class RegionExtents {}; }
Ted Kremenekae189ec2008-12-24 01:05:03 +0000117static int RegionExtentsIndex = 0;
Zhongxing Xu9165ed62008-11-24 09:44:56 +0000118namespace clang {
Ted Kremenekae189ec2008-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 Xu9165ed62008-11-24 09:44:56 +0000123}
124
Ted Kremenekae189ec2008-12-24 01:05:03 +0000125//===----------------------------------------------------------------------===//
Ted Kremenek1f22aa72009-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 Stump11289f42009-09-09 15:08:12 +0000132
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000133 return ty->isIntegerType() && ty->isScalarType() &&
134 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
135}
136
137//===----------------------------------------------------------------------===//
Ted Kremenekae189ec2008-12-24 01:05:03 +0000138// Main RegionStore logic.
139//===----------------------------------------------------------------------===//
Ted Kremenek677779a2008-12-04 02:08:27 +0000140
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000141namespace {
Mike Stump11289f42009-09-09 15:08:12 +0000142
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000143class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000144 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump11289f42009-09-09 15:08:12 +0000145 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000146 SetTy::Factory F;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000147 Map M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000148public:
Ted Kremenek844a7292009-08-05 19:09:24 +0000149 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000150 Map::iterator I = M.find(Parent);
Ted Kremenek844a7292009-08-05 19:09:24 +0000151
152 if (I == M.end()) {
Ted Kremenek68c1f012009-08-05 05:31:02 +0000153 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenek844a7292009-08-05 19:09:24 +0000154 return true;
155 }
156
157 I->second = F.Add(I->second, SubRegion);
158 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000159 }
Mike Stump11289f42009-09-09 15:08:12 +0000160
Ted Kremenekfa417142009-08-06 01:20:57 +0000161 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000163 ~RegionStoreSubRegionMap() {}
Mike Stump11289f42009-09-09 15:08:12 +0000164
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000165 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin612e3802009-11-10 01:17:45 +0000166 Map::const_iterator I = M.find(Parent);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000167
168 if (I == M.end())
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000169 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000170
Ted Kremenek8dc671c2009-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 Kremenek4c8a5812009-03-03 02:51:43 +0000175 return false;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000176 }
Mike Stump11289f42009-09-09 15:08:12 +0000177
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000178 return true;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000179 }
Mike Stump11289f42009-09-09 15:08:12 +0000180
Ted Kremenek1f22aa72009-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 Stump11289f42009-09-09 15:08:12 +0000188};
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000189
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000190class RegionStoreManager : public StoreManager {
Ted Kremenek4533a552009-06-16 22:36:44 +0000191 const RegionStoreFeatures Features;
Ted Kremenek2c85f172009-08-06 04:50:20 +0000192 RegionBindings::Factory RBFactory;
Ted Kremenekcc224242009-09-29 06:35:00 +0000193
194 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
195 SMCache SC;
196
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000197public:
Mike Stump11289f42009-09-09 15:08:12 +0000198 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenek43015262009-07-29 21:43:22 +0000199 : StoreManager(mgr),
Ted Kremenek4533a552009-06-16 22:36:44 +0000200 Features(f),
Ted Kremenek608677a2009-08-21 23:25:54 +0000201 RBFactory(mgr.getAllocator()) {}
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000202
Ted Kremenekcc224242009-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 Xud9959ae2008-10-08 02:50:44 +0000207
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000208 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump11289f42009-09-09 15:08:12 +0000209
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000210 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump11289f42009-09-09 15:08:12 +0000211
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000212 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
213 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000214 /// getDefaultBinding - Returns an SVal* representing an optional default
215 /// binding associated with a region and its subregions.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000216 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek439a6d12009-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 Stump11289f42009-09-09 15:08:12 +0000224
Ted Kremenek2907ab72008-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 Xu7d6387b2009-10-14 03:33:08 +0000229 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000230
Ted Kremenek2907ab72008-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 Xu7d6387b2009-10-14 03:33:08 +0000235 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000236
Ted Kremenek2907ab72008-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 Xu7d6387b2009-10-14 03:33:08 +0000240 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump11289f42009-09-09 15:08:12 +0000241
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000242 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000243
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000244 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump11289f42009-09-09 15:08:12 +0000245
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000246 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000247
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000248 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000249
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000250
Ted Kremenek2907ab72008-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 Xua865b792009-03-30 05:55:46 +0000257 SVal ArrayToPointer(Loc Array);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000258
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000259 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000260 NonLoc R, QualType resultTy);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000261
Mike Stump11289f42009-09-09 15:08:12 +0000262 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek608677a2009-08-21 23:25:54 +0000263 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000264 }
Ted Kremenek608677a2009-08-21 23:25:54 +0000265
Ted Kremenek609df302009-06-17 22:02:04 +0000266 //===-------------------------------------------------------------------===//
267 // Binding values to regions.
268 //===-------------------------------------------------------------------===//
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000269
Ted Kremenekbca70672009-07-29 18:16:25 +0000270 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek1eb68092009-10-16 00:30:49 +0000271 const Expr *E, unsigned Count,
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000272 InvalidatedSymbols *IS) {
273 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
274 }
275
276 const GRState *InvalidateRegions(const GRState *state,
277 const MemRegion * const *Begin,
278 const MemRegion * const *End,
279 const Expr *E, unsigned Count,
280 InvalidatedSymbols *IS);
Mike Stump11289f42009-09-09 15:08:12 +0000281
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000282private:
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000283 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremenekfa417142009-08-06 01:20:57 +0000284 RegionStoreSubRegionMap &M);
Mike Stump11289f42009-09-09 15:08:12 +0000285
286public:
Ted Kremenek609df302009-06-17 22:02:04 +0000287 const GRState *Bind(const GRState *state, Loc LV, SVal V);
288
289 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek04af9f22009-12-07 22:05:27 +0000290 const CompoundLiteralExpr* CL,
291 const LocationContext *LC,
292 SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000293
Ted Kremenekb006b822009-11-04 00:09:15 +0000294 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
295 SVal InitVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000296
Ted Kremenekb006b822009-11-04 00:09:15 +0000297 const GRState *BindDeclWithNoInit(const GRState *state,
298 const VarRegion *) {
Ted Kremenek609df302009-06-17 22:02:04 +0000299 return state;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000300 }
Zhongxing Xu83aff702008-10-21 05:29:26 +0000301
Ted Kremenek609df302009-06-17 22:02:04 +0000302 /// BindStruct - Bind a compound value to a structure.
303 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek609df302009-06-17 22:02:04 +0000305 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000306
307 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000308 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000309
Ted Kremenek609df302009-06-17 22:02:04 +0000310 Store Remove(Store store, Loc LV);
311
312 //===------------------------------------------------------------------===//
313 // Loading values from regions.
314 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenek609df302009-06-17 22:02:04 +0000316 /// The high level logic for this method is this:
317 /// Retrieve (L)
318 /// if L has binding
319 /// return L's binding
320 /// else if L is in killset
321 /// return unknown
322 /// else
323 /// if L is on stack or heap
324 /// return undefined
325 /// else
326 /// return symbolic
Ted Kremenekac7c7242009-07-21 21:03:30 +0000327 SValuator::CastResult Retrieve(const GRState *state, Loc L,
328 QualType T = QualType());
Zhongxing Xue67ea5c2009-06-25 04:50:44 +0000329
Ted Kremenek48029552009-07-15 06:09:28 +0000330 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xu2d160732009-06-25 05:29:39 +0000331
Ted Kremenek48029552009-07-15 06:09:28 +0000332 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenek48029552009-07-15 06:09:28 +0000334 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenekfe12f882009-07-21 00:12:07 +0000336 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek834e2f62009-07-20 22:58:02 +0000338 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000339
Ted Kremenek040e3b92009-08-06 22:33:36 +0000340 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
341 QualType Ty, const MemRegion *superR);
Mike Stump11289f42009-09-09 15:08:12 +0000342
Ted Kremenek609df302009-06-17 22:02:04 +0000343 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump11289f42009-09-09 15:08:12 +0000344 /// struct copy:
345 /// struct s x, y;
Ted Kremenek609df302009-06-17 22:02:04 +0000346 /// x = y;
347 /// y's value is retrieved by this method.
348 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenek609df302009-06-17 22:02:04 +0000350 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000351
Ted Kremenekfa417142009-08-06 01:20:57 +0000352 std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +0000353 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenekfa417142009-08-06 01:20:57 +0000355 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
356 const GRState *state,
357 const TypedRegion *R);
Ted Kremenek609df302009-06-17 22:02:04 +0000358
Ted Kremenek267e45a2009-09-24 04:11:44 +0000359 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
360 QualType T);
361
Ted Kremenek609df302009-06-17 22:02:04 +0000362 //===------------------------------------------------------------------===//
363 // State pruning.
364 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenek609df302009-06-17 22:02:04 +0000366 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
367 /// It returns a new Store with these values removed.
Ted Kremenekcee28a42009-08-02 04:45:08 +0000368 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek609df302009-06-17 22:02:04 +0000369 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
370
Zhongxing Xudaa41762009-10-13 02:24:55 +0000371 const GRState *EnterStackFrame(const GRState *state,
372 const StackFrameContext *frame);
373
Ted Kremenek609df302009-06-17 22:02:04 +0000374 //===------------------------------------------------------------------===//
375 // Region "extents".
376 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000377
Ted Kremenek609df302009-06-17 22:02:04 +0000378 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xu383c2732009-11-12 02:48:32 +0000379 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
380 const MemRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000381
382 //===------------------------------------------------------------------===//
Ted Kremenek609df302009-06-17 22:02:04 +0000383 // Utility methods.
384 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek2c85f172009-08-06 04:50:20 +0000386 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000387 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000388 }
Zhongxing Xucebb7412008-10-24 01:38:55 +0000389
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000390 void print(Store store, llvm::raw_ostream& Out, const char* nl,
391 const char *sep);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000392
393 void iterBindings(Store store, BindingsHandler& f) {
394 // FIXME: Implement.
395 }
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000396
Ted Kremenek609df302009-06-17 22:02:04 +0000397 // FIXME: Remove.
398 BasicValueFactory& getBasicVals() {
399 return StateMgr.getBasicVals();
400 }
Mike Stump11289f42009-09-09 15:08:12 +0000401
Ted Kremenek609df302009-06-17 22:02:04 +0000402 // FIXME: Remove.
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000403 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000404};
405
406} // end anonymous namespace
407
Ted Kremenek4533a552009-06-16 22:36:44 +0000408//===----------------------------------------------------------------------===//
409// RegionStore creation.
410//===----------------------------------------------------------------------===//
411
412StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
413 RegionStoreFeatures F = maximal_features_tag();
414 return new RegionStoreManager(StMgr, F);
415}
416
417StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
418 RegionStoreFeatures F = minimal_features_tag();
419 F.enableFields(true);
420 return new RegionStoreManager(StMgr, F);
Ted Kremenek6779f892008-10-24 01:04:59 +0000421}
422
Ted Kremenekfa417142009-08-06 01:20:57 +0000423void
424RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump11289f42009-09-09 15:08:12 +0000425 const SubRegion *R) {
Ted Kremenekfa417142009-08-06 01:20:57 +0000426 const MemRegion *superR = R->getSuperRegion();
427 if (add(superR, R))
428 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump11289f42009-09-09 15:08:12 +0000429 WL.push_back(sr);
Ted Kremenekfa417142009-08-06 01:20:57 +0000430}
431
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000432RegionStoreSubRegionMap*
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000433RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
434 RegionBindings B = GetRegionBindings(store);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000435 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump11289f42009-09-09 15:08:12 +0000436
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000437 llvm::SmallVector<const SubRegion*, 10> WL;
438
Ted Kremenek2c85f172009-08-06 04:50:20 +0000439 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenekfa417142009-08-06 01:20:57 +0000440 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
441 M->process(WL, R);
Mike Stump11289f42009-09-09 15:08:12 +0000442
Mike Stump11289f42009-09-09 15:08:12 +0000443 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000444 // don't have direct bindings but are super regions of those that do.
445 while (!WL.empty()) {
446 const SubRegion *R = WL.back();
447 WL.pop_back();
Ted Kremenekfa417142009-08-06 01:20:57 +0000448 M->process(WL, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000449 }
450
Ted Kremenek9f276d62009-03-03 19:02:42 +0000451 return M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000452}
Ted Kremenek2907ab72008-12-24 07:46:32 +0000453
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000454SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000455 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000456}
457
Ted Kremenek4533a552009-06-16 22:36:44 +0000458//===----------------------------------------------------------------------===//
Ted Kremenekbca70672009-07-29 18:16:25 +0000459// Binding invalidation.
460//===----------------------------------------------------------------------===//
461
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000462void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
463 const MemRegion *R,
464 RegionStoreSubRegionMap &M) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000465 RegionStoreSubRegionMap::iterator I, E;
466
467 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000468 RemoveSubRegionBindings(B, *I, M);
Mike Stump11289f42009-09-09 15:08:12 +0000469
Ted Kremenekfa417142009-08-06 01:20:57 +0000470 B = RBFactory.Remove(B, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000471}
472
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000473const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
474 const MemRegion * const *I,
475 const MemRegion * const *E,
476 const Expr *Ex,
477 unsigned Count,
478 InvalidatedSymbols *IS) {
Ted Kremenekbca70672009-07-29 18:16:25 +0000479 ASTContext& Ctx = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000480
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000481 // Get the mapping of regions -> subregions.
482 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000483 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000484
485 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000486
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000487 llvm::DenseMap<const MemRegion *, unsigned> Visited;
488 llvm::SmallVector<const MemRegion *, 10> WorkList;
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000489
490 for ( ; I != E; ++I) {
491 // Strip away casts.
492 WorkList.push_back((*I)->StripCasts());
493 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000494
495 while (!WorkList.empty()) {
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000496 const MemRegion *R = WorkList.back();
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000497 WorkList.pop_back();
498
499 // Have we visited this region before?
500 unsigned &visited = Visited[R];
501 if (visited)
502 continue;
503 visited = 1;
Mike Stump11289f42009-09-09 15:08:12 +0000504
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000505 // Add subregions to work list.
506 RegionStoreSubRegionMap::iterator I, E;
507 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
508 WorkList.push_back(*I);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000509
510 // Get the old binding. Is it a region? If so, add it to the worklist.
511 if (Optional<SVal> V = getDirectBinding(B, R)) {
512 if (const MemRegion *RV = V->getAsRegion())
513 WorkList.push_back(RV);
Ted Kremenek1eb68092009-10-16 00:30:49 +0000514
515 // A symbol? Mark it touched by the invalidation.
516 if (IS) {
517 if (SymbolRef Sym = V->getAsSymbol())
518 IS->insert(Sym);
519 }
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000520 }
521
Ted Kremenek1eb68092009-10-16 00:30:49 +0000522 // Symbolic region? Mark that symbol touched by the invalidation.
523 if (IS) {
524 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
525 IS->insert(SR->getSymbol());
526 }
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000527
528 // BlockDataRegion? If so, invalidate captured variables that are passed
529 // by reference.
530 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
531 for (BlockDataRegion::referenced_vars_iterator
532 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
533 I != E; ++I) {
534 const VarRegion *VR = *I;
535 if (VR->getDecl()->getAttr<BlocksAttr>())
536 WorkList.push_back(VR);
537 }
538 continue;
539 }
Ted Kremenek1eb68092009-10-16 00:30:49 +0000540
541 // Handle the region itself.
Ted Kremenekd970acb2009-12-16 23:53:37 +0000542 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000543 // Invalidate the region by setting its default value to
544 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000545 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
546 Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000547 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000548 continue;
549 }
Mike Stump11289f42009-09-09 15:08:12 +0000550
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000551 if (!R->isBoundable())
552 continue;
553
554 const TypedRegion *TR = cast<TypedRegion>(R);
555 QualType T = TR->getValueType(Ctx);
556
557 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000558 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
559
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000560 // No record definition. There is nothing we can do.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000561 if (!RD)
562 continue;
563
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000564 // Invalidate the region by setting its default value to
565 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000566 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
567 Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000568 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000569 continue;
570 }
571
572 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
573 // Set the default value of the array to conjured symbol.
574 DefinedOrUnknownSVal V =
575 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000576 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000577 continue;
578 }
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000579
580 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
581 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000582 // For fields and elements whose super region has also been invalidated,
583 // only remove the old binding. The super region will get set with a
584 // default value from which we can lazily derive a new symbolic value.
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000585 B = RBFactory.Remove(B, R);
586 continue;
587 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000588
Ted Kremenek1cbdf6e2009-09-29 03:12:50 +0000589 // Invalidate the binding.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000590 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
591 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000592 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremenekfa417142009-08-06 01:20:57 +0000593 }
594
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000595 // Create a new state with the updated bindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000596 return state->makeWithStore(B.getRoot());
Ted Kremenekbca70672009-07-29 18:16:25 +0000597}
598
599//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000600// getLValueXXX methods.
601//===----------------------------------------------------------------------===//
602
Ted Kremenek2907ab72008-12-24 07:46:32 +0000603/// getLValueString - Returns an SVal representing the lvalue of a
604/// StringLiteral. Within RegionStore a StringLiteral has an
605/// associated StringRegion, and the lvalue of a StringLiteral is the
606/// lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000607SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000608 return loc::MemRegionVal(MRMgr.getStringRegion(S));
609}
610
Ted Kremenek2907ab72008-12-24 07:46:32 +0000611/// getLValueVar - Returns an SVal that represents the lvalue of a
612/// variable. Within RegionStore a variable has an associated
613/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000614SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000615 const LocationContext *LC) {
616 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000617}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000618
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000619SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
620 return getLValueFieldOrIvar(D, Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000621}
622
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000623SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
624 return getLValueFieldOrIvar(D, Base);
Ted Kremenekd3c82762009-03-05 04:50:08 +0000625}
626
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000627SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000628 if (Base.isUnknownOrUndef())
629 return Base;
630
631 Loc BaseL = cast<Loc>(Base);
632 const MemRegion* BaseR = 0;
633
634 switch (BaseL.getSubKind()) {
635 case loc::MemRegionKind:
636 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
637 break;
638
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000639 case loc::GotoLabelKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000640 // These are anormal cases. Flag an undefined value.
641 return UndefinedVal();
642
643 case loc::ConcreteIntKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000644 // While these seem funny, this can happen through casts.
645 // FIXME: What we should return is the field offset. For example,
646 // add the field offset to the integer value. That way funny things
647 // like this work properly: &(((struct foo *) 0xa)->f)
648 return Base;
649
650 default:
Zhongxing Xue79a4e62008-11-07 08:57:30 +0000651 assert(0 && "Unhandled Base.");
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000652 return Base;
653 }
Mike Stump11289f42009-09-09 15:08:12 +0000654
Ted Kremenekd3c82762009-03-05 04:50:08 +0000655 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
656 // of FieldDecl.
657 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
658 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000659
Ted Kremenekd3c82762009-03-05 04:50:08 +0000660 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000661}
662
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000663SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
664 SVal Base) {
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000665
Ted Kremenek06032222009-03-09 22:44:49 +0000666 // If the base is an unknown or undefined value, just return it back.
667 // FIXME: For absolute pointer addresses, we just return that value back as
668 // well, although in reality we should return the offset added to that
669 // value.
670 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu36d4ade2008-10-27 12:23:17 +0000671 return Base;
672
Ted Kremenek92d48a72009-01-22 20:27:48 +0000673 // Only handle integer offsets... for now.
674 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000675 return UnknownVal();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000676
Zhongxing Xu7c382642009-05-09 13:20:07 +0000677 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000678
679 // Pointer of any type can be cast and used as array base.
680 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump11289f42009-09-09 15:08:12 +0000681
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000682 // Convert the offset to the appropriate size and signedness.
683 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000684
Ted Kremenek92d48a72009-01-22 20:27:48 +0000685 if (!ElemR) {
686 //
687 // If the base region is not an ElementRegion, create one.
688 // This can happen in the following example:
689 //
690 // char *p = __builtin_alloc(10);
691 // p[1] = 8;
692 //
Zhongxing Xu7c382642009-05-09 13:20:07 +0000693 // Observe that 'p' binds to an AllocaRegion.
Ted Kremenek92d48a72009-01-22 20:27:48 +0000694 //
Ted Kremenek02e50892009-05-04 06:18:28 +0000695 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000696 BaseRegion, getContext()));
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenek92d48a72009-01-22 20:27:48 +0000699 SVal BaseIdx = ElemR->getIndex();
Mike Stump11289f42009-09-09 15:08:12 +0000700
Ted Kremenek92d48a72009-01-22 20:27:48 +0000701 if (!isa<nonloc::ConcreteInt>(BaseIdx))
702 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000703
Ted Kremenek92d48a72009-01-22 20:27:48 +0000704 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
705 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
706 assert(BaseIdxI.isSigned());
Mike Stump11289f42009-09-09 15:08:12 +0000707
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000708 // Compute the new index.
709 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump11289f42009-09-09 15:08:12 +0000710
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000711 // Construct the new ElementRegion.
712 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000713 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump11289f42009-09-09 15:08:12 +0000714 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000715}
716
Ted Kremenek4533a552009-06-16 22:36:44 +0000717//===----------------------------------------------------------------------===//
718// Extents for regions.
719//===----------------------------------------------------------------------===//
720
Zhongxing Xu383c2732009-11-12 02:48:32 +0000721DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
722 const MemRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +0000723
Ted Kremenek94575aa2009-07-10 22:30:06 +0000724 switch (R->getKind()) {
Ted Kremenek04af9f22009-12-07 22:05:27 +0000725 case MemRegion::GenericMemSpaceRegionKind:
726 case MemRegion::StackLocalsSpaceRegionKind:
727 case MemRegion::StackArgumentsSpaceRegionKind:
728 case MemRegion::HeapSpaceRegionKind:
729 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000730 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000731 assert(0 && "Cannot index into a MemSpace");
Mike Stump11289f42009-09-09 15:08:12 +0000732 return UnknownVal();
733
Ted Kremenek10a50e72009-11-25 01:32:22 +0000734 case MemRegion::FunctionTextRegionKind:
735 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000736 case MemRegion::BlockDataRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000737 // Technically this can happen if people do funny things with casts.
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000738 return UnknownVal();
Ted Kremenek94575aa2009-07-10 22:30:06 +0000739
740 // Not yet handled.
741 case MemRegion::AllocaRegionKind:
742 case MemRegion::CompoundLiteralRegionKind:
743 case MemRegion::ElementRegionKind:
744 case MemRegion::FieldRegionKind:
745 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000746 case MemRegion::SymbolicRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000747 case MemRegion::CXXObjectRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000748 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000749
Ted Kremenek94575aa2009-07-10 22:30:06 +0000750 case MemRegion::StringRegionKind: {
751 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000752 // We intentionally made the size value signed because it participates in
Ted Kremenek94575aa2009-07-10 22:30:06 +0000753 // operations with signed indices.
754 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000755 }
Mike Stump11289f42009-09-09 15:08:12 +0000756
Ted Kremenek94575aa2009-07-10 22:30:06 +0000757 case MemRegion::VarRegionKind: {
758 const VarRegion* VR = cast<VarRegion>(R);
759 // Get the type of the variable.
760 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000761
Ted Kremenek94575aa2009-07-10 22:30:06 +0000762 // FIXME: Handle variable-length arrays.
763 if (isa<VariableArrayType>(T))
764 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000765
Ted Kremenek94575aa2009-07-10 22:30:06 +0000766 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
767 // return the size as signed integer.
768 return ValMgr.makeIntVal(CAT->getSize(), false);
769 }
Ted Kremenekca7935d2009-08-02 05:15:23 +0000770
Ted Kremenek94575aa2009-07-10 22:30:06 +0000771 // Clients can use ordinary variables as if they were arrays. These
772 // essentially are arrays of size 1.
773 return ValMgr.makeIntVal(1, false);
Zhongxing Xuea8c48d2009-05-06 11:51:48 +0000774 }
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Ted Kremenek94575aa2009-07-10 22:30:06 +0000777 assert(0 && "Unreachable");
Ted Kremenek47ad37d2009-01-06 19:12:06 +0000778 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000779}
780
Ted Kremenek609df302009-06-17 22:02:04 +0000781const GRState *RegionStoreManager::setExtent(const GRState *state,
782 const MemRegion *region,
783 SVal extent) {
784 return state->set<RegionExtents>(region, extent);
Ted Kremenek4533a552009-06-16 22:36:44 +0000785}
786
787//===----------------------------------------------------------------------===//
788// Location and region casting.
789//===----------------------------------------------------------------------===//
790
Ted Kremenek2907ab72008-12-24 07:46:32 +0000791/// ArrayToPointer - Emulates the "decay" of an array to a pointer
792/// type. 'Array' represents the lvalue of the array being decayed
793/// to a pointer, and the returned SVal represents the decayed
794/// version of that lvalue (i.e., a pointer to the first element of
795/// the array). This is called by GRExprEngine when evaluating casts
796/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000797SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000798 if (!isa<loc::MemRegionVal>(Array))
799 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenekf065b152008-12-13 19:24:37 +0000801 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
802 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000803
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000804 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000805 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000806
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000807 // Strip off typedefs from the ArrayRegion's ValueType.
John McCalla1925362009-09-29 23:03:30 +0000808 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000809 ArrayType *AT = cast<ArrayType>(T);
810 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000811
Ted Kremenekccc22922009-07-16 00:00:11 +0000812 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenek721fcc02009-12-04 00:26:31 +0000813 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
814 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000815}
816
Ted Kremenek4533a552009-06-16 22:36:44 +0000817//===----------------------------------------------------------------------===//
818// Pointer arithmetic.
819//===----------------------------------------------------------------------===//
820
Mike Stump11289f42009-09-09 15:08:12 +0000821SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000822 BinaryOperator::Opcode Op, Loc L, NonLoc R,
823 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000824 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000825 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000826 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000827
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000828 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000829 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000830
Ted Kremenekf6f04612009-07-11 00:58:27 +0000831 switch (MR->getKind()) {
832 case MemRegion::SymbolicRegionKind: {
833 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000834 SymbolRef Sym = SR->getSymbol();
Ted Kremenekd1d60662009-08-25 22:55:09 +0000835 QualType T = Sym->getType(getContext());
836 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Ted Kremenekd1d60662009-08-25 22:55:09 +0000838 if (const PointerType *PT = T->getAs<PointerType>())
839 EleTy = PT->getPointeeType();
840 else
John McCall9dd450b2009-09-21 23:43:11 +0000841 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000842
Ted Kremenekf6f04612009-07-11 00:58:27 +0000843 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
844 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000845 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000846 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000847 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000848 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000849 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000850 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenekf6f04612009-07-11 00:58:27 +0000851 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
852 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000853 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000854 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000855
Ted Kremenekf6f04612009-07-11 00:58:27 +0000856 case MemRegion::ElementRegionKind: {
857 ER = cast<ElementRegion>(MR);
858 break;
859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Ted Kremenekf6f04612009-07-11 00:58:27 +0000861 // Not yet handled.
862 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000863 case MemRegion::StringRegionKind: {
864
865 }
866 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000867 case MemRegion::CompoundLiteralRegionKind:
868 case MemRegion::FieldRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000869 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xu6df9f542009-12-16 11:27:52 +0000870 case MemRegion::CXXObjectRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000871 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000872
Ted Kremenek10a50e72009-11-25 01:32:22 +0000873 case MemRegion::FunctionTextRegionKind:
874 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000875 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000876 // Technically this can happen if people do funny things with casts.
877 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000878
Ted Kremenek04af9f22009-12-07 22:05:27 +0000879 case MemRegion::GenericMemSpaceRegionKind:
880 case MemRegion::StackLocalsSpaceRegionKind:
881 case MemRegion::StackArgumentsSpaceRegionKind:
882 case MemRegion::HeapSpaceRegionKind:
883 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +0000884 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000885 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
886 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000887 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000888
Zhongxing Xue7d14932009-03-02 07:52:23 +0000889 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000890 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +0000891
Ted Kremenek8ec57712009-10-06 01:39:48 +0000892 // For now, only support:
893 // (a) concrete integer indices that can easily be resolved
894 // (b) 0 + symbolic index
895 if (Base) {
896 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
897 // FIXME: Should use SValuator here.
898 SVal NewIdx =
899 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000900 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +0000901 const MemRegion* NewER =
902 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
903 ER->getSuperRegion(), getContext());
904 return ValMgr.makeLoc(NewER);
905 }
906 if (0 == Base->getValue()) {
907 const MemRegion* NewER =
908 MRMgr.getElementRegion(ER->getElementType(), R,
909 ER->getSuperRegion(), getContext());
910 return ValMgr.makeLoc(NewER);
911 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000914 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000915}
916
Ted Kremenek4533a552009-06-16 22:36:44 +0000917//===----------------------------------------------------------------------===//
918// Loading values from regions.
919//===----------------------------------------------------------------------===//
920
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000921Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
922 const MemRegion *R) {
923 if (const BindingVal *BV = B.lookup(R))
924 return Optional<SVal>::create(BV->getDirectValue());
925
926 return Optional<SVal>();
927}
928
929Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000930 const MemRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +0000931
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000932 if (R->isBoundable())
933 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
934 if (TR->getValueType(getContext())->isUnionType())
935 return UnknownVal();
936
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000937 if (BindingVal const *V = B.lookup(R))
938 return Optional<SVal>::create(V->getDefaultValue());
939
940 return Optional<SVal>();
941}
942
943Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
944 const MemRegion *R) {
945 if (const BindingVal *BV = B.lookup(R))
946 return Optional<SVal>::create(BV->getValue());
947
948 return Optional<SVal>();
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000949}
950
Ted Kremeneke6fea682009-07-15 02:31:43 +0000951static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
952 RTy = Ctx.getCanonicalType(RTy);
953 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump11289f42009-09-09 15:08:12 +0000954
Ted Kremeneke6fea682009-07-15 02:31:43 +0000955 if (RTy == UsedTy)
956 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000957
958
Ted Kremenek834e2f62009-07-20 22:58:02 +0000959 // Recursively check the types. We basically want to see if a pointer value
Mike Stump11289f42009-09-09 15:08:12 +0000960 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek834e2f62009-07-20 22:58:02 +0000961 // represents a reinterpretation.
962 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump11289f42009-09-09 15:08:12 +0000963 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000964 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek834e2f62009-07-20 22:58:02 +0000965
966 return PUsedTy && PRTy &&
967 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump11289f42009-09-09 15:08:12 +0000968 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek834e2f62009-07-20 22:58:02 +0000969 }
970
971 return true;
Ted Kremeneke6fea682009-07-15 02:31:43 +0000972}
973
Ted Kremenek267e45a2009-09-24 04:11:44 +0000974const ElementRegion *
975RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
976 ASTContext &Ctx = getContext();
977 SVal idx = ValMgr.makeZeroArrayIndex();
978 assert(!T.isNull());
979 return MRMgr.getElementRegion(T, idx, SR, Ctx);
980}
981
982
983
Ted Kremenekac7c7242009-07-21 21:03:30 +0000984SValuator::CastResult
985RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek609df302009-06-17 22:02:04 +0000986
Zhongxing Xu83aff702008-10-21 05:29:26 +0000987 assert(!isa<UnknownVal>(L) && "location unknown");
988 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek9158fb72009-12-15 23:23:27 +0000989
Ted Kremenek2907ab72008-12-24 07:46:32 +0000990 // FIXME: Is this even possible? Shouldn't this be treated as a null
991 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000992 if (isa<loc::ConcreteInt>(L))
Ted Kremenekac7c7242009-07-21 21:03:30 +0000993 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek9158fb72009-12-15 23:23:27 +0000994
Ted Kremenek609df302009-06-17 22:02:04 +0000995 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000996
Zhongxing Xu1075cc02009-05-20 09:18:48 +0000997 // FIXME: return symbolic value for these cases.
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000998 // Example:
999 // void f(int* p) { int x = *p; }
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001000 // char* p = alloca();
1001 // read(p);
1002 // c = *p;
Ted Kremenek0c37d192009-07-14 20:48:22 +00001003 if (isa<AllocaRegion>(MR))
Ted Kremenekac7c7242009-07-21 21:03:30 +00001004 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001005
Ted Kremenek267e45a2009-09-24 04:11:44 +00001006 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1007 MR = GetElementZeroRegion(SR, T);
Mike Stump11289f42009-09-09 15:08:12 +00001008
Ted Kremenek0bb32e32009-08-03 21:41:46 +00001009 if (isa<CodeTextRegion>(MR))
1010 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001011
Ted Kremenek2907ab72008-12-24 07:46:32 +00001012 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1013 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +00001014 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001015 QualType RTy = R->getValueType(getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001016
Ted Kremenek2907ab72008-12-24 07:46:32 +00001017 // FIXME: We should eventually handle funny addressing. e.g.:
1018 //
1019 // int x = ...;
1020 // int *p = &x;
1021 // char *q = (char*) p;
1022 // char c = *q; // returns the first byte of 'x'.
1023 //
1024 // Such funny addressing will occur due to layering of regions.
1025
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001026#if 0
Ted Kremeneke6fea682009-07-15 02:31:43 +00001027 ASTContext &Ctx = getContext();
1028 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001029 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1030 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001031 RTy = T;
Ted Kremenek57fa7e32009-07-15 04:23:32 +00001032 assert(Ctx.getCanonicalType(RTy) ==
1033 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump11289f42009-09-09 15:08:12 +00001034 }
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001035#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001036
Zhongxing Xuce270a62009-03-09 09:15:51 +00001037 if (RTy->isStructureType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001038 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump11289f42009-09-09 15:08:12 +00001039
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001040 // FIXME: Handle unions.
1041 if (RTy->isUnionType())
1042 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001043
1044 if (RTy->isArrayType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001045 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001046
Zhongxing Xuce270a62009-03-09 09:15:51 +00001047 // FIXME: handle Vector types.
1048 if (RTy->isVectorType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001049 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu0628f532009-06-28 14:16:39 +00001050
1051 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001052 return SValuator::CastResult(state,
1053 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu0628f532009-06-28 14:16:39 +00001054
1055 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001056 return SValuator::CastResult(state,
1057 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump11289f42009-09-09 15:08:12 +00001058
Ted Kremenek834e2f62009-07-20 22:58:02 +00001059 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001060 return SValuator::CastResult(state,
1061 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump11289f42009-09-09 15:08:12 +00001062
Ted Kremenekfe12f882009-07-21 00:12:07 +00001063 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001064 return SValuator::CastResult(state,
1065 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek834e2f62009-07-20 22:58:02 +00001066
Ted Kremenek2c85f172009-08-06 04:50:20 +00001067 RegionBindings B = GetRegionBindings(state->getStore());
1068 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001069
1070 // Check if the region has a binding.
1071 if (V)
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001072 if (SVal const *SV = V->getValue())
1073 return SValuator::CastResult(state, *SV);
Ted Kremenek2907ab72008-12-24 07:46:32 +00001074
Ted Kremenek2907ab72008-12-24 07:46:32 +00001075 // The location does not have a bound value. This means that it has
1076 // the value it had upon its creation and/or entry to the analyzed
1077 // function/method. These are either symbolic values or 'undefined'.
1078
Ted Kremenek920ad712009-07-22 04:35:42 +00001079#if HEAP_UNDEFINED
Ted Kremenek2d99f972009-06-23 18:17:08 +00001080 if (R->hasHeapOrStackStorage()) {
Ted Kremenek920ad712009-07-22 04:35:42 +00001081#else
1082 if (R->hasStackStorage()) {
1083#endif
Ted Kremenek2907ab72008-12-24 07:46:32 +00001084 // All stack variables are considered to have undefined values
1085 // upon creation. All heap allocated blocks are considered to
1086 // have undefined values as well unless they are explicitly bound
1087 // to specific values.
Ted Kremenekac7c7242009-07-21 21:03:30 +00001088 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek2907ab72008-12-24 07:46:32 +00001089 }
1090
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001091 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001092 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(R, RTy));
Zhongxing Xu83aff702008-10-21 05:29:26 +00001093}
Mike Stump11289f42009-09-09 15:08:12 +00001094
Ted Kremenekfa417142009-08-06 01:20:57 +00001095std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001096RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001097 if (Optional<SVal> OV = getDirectBinding(B, R))
1098 if (const nonloc::LazyCompoundVal *V =
1099 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1100 return std::make_pair(V->getState(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001101
Ted Kremenekfa417142009-08-06 01:20:57 +00001102 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1103 const std::pair<const GRState *, const MemRegion *> &X =
1104 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001105
Ted Kremenekfa417142009-08-06 01:20:57 +00001106 if (X.first)
1107 return std::make_pair(X.first,
1108 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001109 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001110 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1111 const std::pair<const GRState *, const MemRegion *> &X =
1112 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001113
Ted Kremenekfa417142009-08-06 01:20:57 +00001114 if (X.first)
1115 return std::make_pair(X.first,
1116 MRMgr.getFieldRegionWithSuper(FR, X.second));
1117 }
1118
1119 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1120}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001121
Zhongxing Xu2d160732009-06-25 05:29:39 +00001122SVal RegionStoreManager::RetrieveElement(const GRState* state,
1123 const ElementRegion* R) {
1124 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001125 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001126 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001127 return *V;
1128
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001129 const MemRegion* superR = R->getSuperRegion();
1130
Zhongxing Xu2d160732009-06-25 05:29:39 +00001131 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001132 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek228539f2009-09-29 16:36:48 +00001133 // FIXME: Handle loads from strings where the literal is treated as
1134 // an integer, e.g., *((unsigned int*)"hello")
1135 ASTContext &Ctx = getContext();
Douglas Gregor4ef1d402009-11-09 22:08:55 +00001136 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001137 if (T != Ctx.getCanonicalType(R->getElementType()))
1138 return UnknownVal();
1139
Zhongxing Xu2d160732009-06-25 05:29:39 +00001140 const StringLiteral *Str = StrR->getStringLiteral();
1141 SVal Idx = R->getIndex();
1142 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1143 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001144 int64_t byteLength = Str->getByteLength();
Ted Kremenekb5850f92009-09-05 17:59:01 +00001145 if (i > byteLength) {
1146 // Buffer overflow checking in GRExprEngine should handle this case,
1147 // but we shouldn't rely on it to not overflow here if that checking
1148 // is disabled.
1149 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001150 }
Ted Kremenekb5850f92009-09-05 17:59:01 +00001151 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001152 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001153 }
1154 }
Mike Stump11289f42009-09-09 15:08:12 +00001155
Ted Kremenek040e3b92009-08-06 22:33:36 +00001156 // Check if the immediate super region has a direct binding.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001157 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneke6fea682009-07-15 02:31:43 +00001158 if (SymbolRef parentSym = V->getAsSymbol())
1159 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek920ad712009-07-22 04:35:42 +00001160
1161 if (V->isUnknownOrUndef())
1162 return *V;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001163
1164 // Handle LazyCompoundVals for the immediate super region. Other cases
1165 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump11289f42009-09-09 15:08:12 +00001166 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek040e3b92009-08-06 22:33:36 +00001167 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump11289f42009-09-09 15:08:12 +00001168
Ted Kremenek040e3b92009-08-06 22:33:36 +00001169 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1170 return RetrieveElement(LCV->getState(), R);
1171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Ted Kremeneke6fea682009-07-15 02:31:43 +00001173 // Other cases: give up.
Zhongxing Xu61e66922009-07-03 06:11:41 +00001174 return UnknownVal();
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001175 }
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001176
Ted Kremenek040e3b92009-08-06 22:33:36 +00001177 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001178}
1179
Mike Stump11289f42009-09-09 15:08:12 +00001180SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001181 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001182
1183 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001184 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001185 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001186 return *V;
1187
Ted Kremenek040e3b92009-08-06 22:33:36 +00001188 QualType Ty = R->getValueType(getContext());
1189 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1190}
Mike Stump11289f42009-09-09 15:08:12 +00001191
Ted Kremenek040e3b92009-08-06 22:33:36 +00001192SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1193 const TypedRegion *R,
1194 QualType Ty,
1195 const MemRegion *superR) {
1196
Mike Stump11289f42009-09-09 15:08:12 +00001197 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001198 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001199
Ted Kremenek040e3b92009-08-06 22:33:36 +00001200 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001201
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001202 while (superR) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001203 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001204 if (SymbolRef parentSym = D->getAsSymbol())
1205 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001207 if (D->isZeroConstant())
1208 return ValMgr.makeZeroVal(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001209
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001210 if (D->isUnknown())
1211 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001213 assert(0 && "Unknown default value");
1214 }
Mike Stump11289f42009-09-09 15:08:12 +00001215
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001216 // If our super region is a field or element itself, walk up the region
1217 // hierarchy to see if there is a default value installed in an ancestor.
1218 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1219 superR = cast<SubRegion>(superR)->getSuperRegion();
1220 continue;
1221 }
Mike Stump11289f42009-09-09 15:08:12 +00001222
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001223 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001224 }
Mike Stump11289f42009-09-09 15:08:12 +00001225
Ted Kremenekfa417142009-08-06 01:20:57 +00001226 // Lazy binding?
1227 const GRState *lazyBindingState = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001228 const MemRegion *lazyBindingRegion = NULL;
1229 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Ted Kremenekfa417142009-08-06 01:20:57 +00001231 if (lazyBindingState) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001232 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump11289f42009-09-09 15:08:12 +00001233
Ted Kremenek040e3b92009-08-06 22:33:36 +00001234 if (isa<ElementRegion>(R))
1235 return RetrieveElement(lazyBindingState,
1236 cast<ElementRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001237
Ted Kremenekfa417142009-08-06 01:20:57 +00001238 return RetrieveField(lazyBindingState,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001239 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001240 }
1241
Ted Kremenek040e3b92009-08-06 22:33:36 +00001242 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump11289f42009-09-09 15:08:12 +00001243
Ted Kremenek040e3b92009-08-06 22:33:36 +00001244 if (isa<ElementRegion>(R)) {
1245 // Currently we don't reason specially about Clang-style vectors. Check
1246 // if superR is a vector and if so return Unknown.
1247 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1248 if (typedSuperR->getValueType(getContext())->isVectorType())
1249 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001250 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001251 }
Mike Stump11289f42009-09-09 15:08:12 +00001252
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001253 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001254 }
Mike Stump11289f42009-09-09 15:08:12 +00001255
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001256 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001257 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001258}
Mike Stump11289f42009-09-09 15:08:12 +00001259
1260SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek48029552009-07-15 06:09:28 +00001261 const ObjCIvarRegion* R) {
1262
Ted Kremenek48029552009-07-15 06:09:28 +00001263 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001264 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek48029552009-07-15 06:09:28 +00001265
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001266 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001267 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001268
Ted Kremenek48029552009-07-15 06:09:28 +00001269 const MemRegion *superR = R->getSuperRegion();
1270
Ted Kremenek481c1212009-10-20 01:20:57 +00001271 // Check if the super region has a default binding.
1272 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001273 if (SymbolRef parentSym = V->getAsSymbol())
1274 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001275
Ted Kremenek48029552009-07-15 06:09:28 +00001276 // Other cases: give up.
1277 return UnknownVal();
1278 }
Mike Stump11289f42009-09-09 15:08:12 +00001279
Ted Kremenek834e2f62009-07-20 22:58:02 +00001280 return RetrieveLazySymbol(state, R);
1281}
1282
Ted Kremenekfe12f882009-07-21 00:12:07 +00001283SVal RegionStoreManager::RetrieveVar(const GRState *state,
1284 const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001285
Ted Kremenekfe12f882009-07-21 00:12:07 +00001286 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001287 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001288
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001289 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001290 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001291
Ted Kremenekfe12f882009-07-21 00:12:07 +00001292 // Lazily derive a value for the VarRegion.
1293 const VarDecl *VD = R->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001294
Ted Kremenekf6d9ceb2009-12-11 06:43:27 +00001295 if (R->hasGlobalsOrParametersStorage() ||
1296 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek9158fb72009-12-15 23:23:27 +00001297 return ValMgr.getRegionValueSymbolVal(R, VD->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001298
Ted Kremenekfe12f882009-07-21 00:12:07 +00001299 return UndefinedVal();
1300}
1301
Mike Stump11289f42009-09-09 15:08:12 +00001302SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek834e2f62009-07-20 22:58:02 +00001303 const TypedRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001304
Ted Kremenek834e2f62009-07-20 22:58:02 +00001305 QualType valTy = R->getValueType(getContext());
Ted Kremenek920ad712009-07-22 04:35:42 +00001306
Ted Kremenek48029552009-07-15 06:09:28 +00001307 // All other values are symbolic.
Ted Kremenek9158fb72009-12-15 23:23:27 +00001308 return ValMgr.getRegionValueSymbolVal(R, valTy);
Ted Kremenek48029552009-07-15 06:09:28 +00001309}
1310
Mike Stump11289f42009-09-09 15:08:12 +00001311SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1312 const TypedRegion* R) {
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001313 QualType T = R->getValueType(getContext());
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001314 assert(T->isStructureType());
1315
Zhongxing Xud85a9912009-06-11 07:27:30 +00001316 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001317 RecordDecl* RD = RT->getDecl();
1318 assert(RD->isDefinition());
Mike Stumpc700b362009-08-06 12:56:50 +00001319 (void)RD;
Ted Kremenekfa417142009-08-06 01:20:57 +00001320#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001321 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1322
Ted Kremenek609df302009-06-17 22:02:04 +00001323 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1324 // reverse iterator, we should implement one.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001325 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001326
Douglas Gregor7a4fad12008-12-11 20:41:00 +00001327 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1328 FieldEnd = Fields.rend();
1329 Field != FieldEnd; ++Field) {
1330 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001331 QualType FTy = (*Field)->getType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001332 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001333 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1334 }
1335
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001336 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001337#else
1338 return ValMgr.makeLazyCompoundVal(state, R);
1339#endif
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001340}
1341
Ted Kremenek609df302009-06-17 22:02:04 +00001342SVal RegionStoreManager::RetrieveArray(const GRState *state,
1343 const TypedRegion * R) {
Ted Kremenekfa417142009-08-06 01:20:57 +00001344#if USE_EXPLICIT_COMPOUND
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001345 QualType T = R->getValueType(getContext());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001346 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1347
1348 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001349 uint64_t size = CAT->getSize().getZExtValue();
1350 for (uint64_t i = 0; i < size; ++i) {
1351 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu838a0db2009-06-16 09:55:50 +00001352 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump11289f42009-09-09 15:08:12 +00001353 getContext());
Ted Kremenek02e50892009-05-04 06:18:28 +00001354 QualType ETy = ER->getElementType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001355 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001356 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1357 }
1358
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001359 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001360#else
1361 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1362 return ValMgr.makeLazyCompoundVal(state, R);
1363#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001364}
1365
Ted Kremenek4533a552009-06-16 22:36:44 +00001366//===----------------------------------------------------------------------===//
1367// Binding values to regions.
1368//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001369
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001370Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001371 const MemRegion* R = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001372
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001373 if (isa<loc::MemRegionVal>(L))
1374 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001375
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001376 if (R) {
Mike Stump11289f42009-09-09 15:08:12 +00001377 RegionBindings B = GetRegionBindings(store);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001378 return RBFactory.Remove(B, R).getRoot();
1379 }
Mike Stump11289f42009-09-09 15:08:12 +00001380
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001381 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001382}
1383
Ted Kremenek609df302009-06-17 22:02:04 +00001384const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001385 if (isa<loc::ConcreteInt>(L))
1386 return state;
1387
Ted Kremenek4533a552009-06-16 22:36:44 +00001388 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001389 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001390
Ted Kremenek4533a552009-06-16 22:36:44 +00001391 // Check if the region is a struct region.
1392 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1393 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001394 return BindStruct(state, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001395
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001396 // Special case: the current region represents a cast and it and the super
1397 // region both have pointer types or intptr_t types. If so, perform the
1398 // bind to the super region.
1399 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump11289f42009-09-09 15:08:12 +00001400 // loads that treat integers as pointers and vis versa.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001401 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1402 if (ER->getIndex().isZeroConstant()) {
1403 if (const TypedRegion *superR =
1404 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1405 ASTContext &Ctx = getContext();
1406 QualType superTy = superR->getValueType(Ctx);
1407 QualType erTy = ER->getValueType(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001408
1409 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001410 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump11289f42009-09-09 15:08:12 +00001411 SValuator::CastResult cr =
1412 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001413 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1414 }
Ted Kremenek25c9c142009-09-21 22:58:52 +00001415 // For now, just invalidate the fields of the struct/union/class.
1416 // FIXME: Precisely handle the fields of the record.
1417 if (superTy->isRecordType())
Ted Kremenek1eb68092009-10-16 00:30:49 +00001418 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001419 }
1420 }
1421 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001422 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1423 // Binding directly to a symbolic region should be treated as binding
1424 // to element 0.
1425 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +00001426
1427 // FIXME: Is this the right way to handle symbols that are references?
1428 if (const PointerType *PT = T->getAs<PointerType>())
1429 T = PT->getPointeeType();
1430 else
1431 T = T->getAs<ReferenceType>()->getPointeeType();
1432
Ted Kremenek267e45a2009-09-24 04:11:44 +00001433 R = GetElementZeroRegion(SR, T);
1434 }
Mike Stump11289f42009-09-09 15:08:12 +00001435
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001436 // Perform the binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001437 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001438 return state->makeWithStore(
1439 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek4533a552009-06-16 22:36:44 +00001440}
1441
Ted Kremenek14536f62009-08-21 22:28:32 +00001442const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekb006b822009-11-04 00:09:15 +00001443 const VarRegion *VR,
Ted Kremenek14536f62009-08-21 22:28:32 +00001444 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001445
Ted Kremenekb006b822009-11-04 00:09:15 +00001446 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001447
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001448 if (T->isArrayType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001449 return BindArray(ST, VR, InitVal);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001450 if (T->isStructureType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001451 return BindStruct(ST, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001452
Ted Kremenek14536f62009-08-21 22:28:32 +00001453 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001454}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001455
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001456// FIXME: this method should be merged into Bind().
Ted Kremenek609df302009-06-17 22:02:04 +00001457const GRState *
1458RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek04af9f22009-12-07 22:05:27 +00001459 const CompoundLiteralExpr *CL,
1460 const LocationContext *LC,
Ted Kremenek609df302009-06-17 22:02:04 +00001461 SVal V) {
Ted Kremenek04af9f22009-12-07 22:05:27 +00001462 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1463 V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001464}
1465
Ted Kremenek439a6d12009-11-19 20:20:24 +00001466const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1467 const MemRegion *R,
1468 QualType T) {
1469 Store store = state->getStore();
1470 RegionBindings B = GetRegionBindings(store);
1471 SVal V;
1472
1473 if (Loc::IsLocType(T))
1474 V = ValMgr.makeNull();
1475 else if (T->isIntegerType())
1476 V = ValMgr.makeZeroVal(T);
1477 else if (T->isStructureType() || T->isArrayType()) {
1478 // Set the default value to a zero constant when it is a structure
1479 // or array. The type doesn't really matter.
1480 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1481 }
1482 else {
1483 return state;
1484 }
1485
1486 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1487 return state->makeWithStore(B.getRoot());
1488}
1489
Ted Kremenek609df302009-06-17 22:02:04 +00001490const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001491 const TypedRegion* R,
Ted Kremenek609df302009-06-17 22:02:04 +00001492 SVal Init) {
1493
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001494 QualType T = R->getValueType(getContext());
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001495 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001496 QualType ElementTy = CAT->getElementType();
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001497
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001498 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001499
1500 // Check if the init expr is a StringLiteral.
1501 if (isa<loc::MemRegionVal>(Init)) {
1502 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1503 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1504 const char* str = S->getStrData();
1505 unsigned len = S->getByteLength();
1506 unsigned j = 0;
1507
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001508 // Copy bytes from the string literal into the target array. Trailing bytes
1509 // in the array that are not covered by the string literal are initialized
1510 // to zero.
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001511 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001512 if (j >= len)
1513 break;
1514
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001515 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001516 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1517 getContext());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001518
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001519 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek609df302009-06-17 22:02:04 +00001520 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001521 }
1522
Ted Kremenek609df302009-06-17 22:02:04 +00001523 return state;
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001524 }
1525
Ted Kremenekfa417142009-08-06 01:20:57 +00001526 // Handle lazy compound values.
1527 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1528 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001529
1530 // Remaining case: explicit compound values.
Ted Kremenek439a6d12009-11-19 20:20:24 +00001531
1532 if (Init.isUnknown())
1533 return setImplicitDefaultValue(state, R, ElementTy);
1534
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001535 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001536 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001537 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001538
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001539 for (; i < size; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001540 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001541 if (VI == VE)
1542 break;
1543
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001544 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001545 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001546
1547 if (CAT->getElementType()->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001548 state = BindStruct(state, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001549 else
Ted Kremenek30030012009-09-22 21:19:14 +00001550 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001551 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001552 }
1553
Ted Kremenek439a6d12009-11-19 20:20:24 +00001554 // If the init list is shorter than the array length, set the
1555 // array default value.
1556 if (i < size)
1557 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001558
Ted Kremenek609df302009-06-17 22:02:04 +00001559 return state;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001560}
1561
Ted Kremenek609df302009-06-17 22:02:04 +00001562const GRState *
1563RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1564 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001565
Ted Kremenek609df302009-06-17 22:02:04 +00001566 if (!Features.supportsFields())
1567 return state;
Mike Stump11289f42009-09-09 15:08:12 +00001568
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001569 QualType T = R->getValueType(getContext());
Zhongxing Xub393b502008-10-31 10:53:01 +00001570 assert(T->isStructureType());
1571
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001572 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001573 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001574
1575 if (!RD->isDefinition())
Ted Kremenek609df302009-06-17 22:02:04 +00001576 return state;
Zhongxing Xub393b502008-10-31 10:53:01 +00001577
Ted Kremenekfa417142009-08-06 01:20:57 +00001578 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001579 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremenekfa417142009-08-06 01:20:57 +00001580 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001581
Ted Kremenek609df302009-06-17 22:02:04 +00001582 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1583 // Ignore them and kill the field values.
1584 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001585 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001586
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001587 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001588 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001589
1590 RecordDecl::field_iterator FI, FE;
1591
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001592 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001593
Zhongxing Xu0442e962009-06-23 05:43:16 +00001594 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001595 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001596
Zhongxing Xub393b502008-10-31 10:53:01 +00001597 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001598 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001599
Ted Kremenek30030012009-09-22 21:19:14 +00001600 if (FTy->isArrayType())
Ted Kremenek609df302009-06-17 22:02:04 +00001601 state = BindArray(state, FR, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001602 else if (FTy->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001603 state = BindStruct(state, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001604 else
1605 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001606 }
1607
Zhongxing Xu0442e962009-06-23 05:43:16 +00001608 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001609 if (FI != FE) {
1610 Store store = state->getStore();
1611 RegionBindings B = GetRegionBindings(store);
1612 B = RBFactory.Add(B, R,
1613 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1614 state = state->makeWithStore(B.getRoot());
1615 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001616
Ted Kremenek609df302009-06-17 22:02:04 +00001617 return state;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001618}
1619
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001620Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1621 RegionBindings B = GetRegionBindings(store);
1622 llvm::OwningPtr<RegionStoreSubRegionMap>
1623 SubRegions(getRegionStoreSubRegionMap(store));
1624 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001625
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001626 // Set the default value of the struct region to "unknown".
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001627 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xucff637a2009-01-13 01:49:57 +00001628
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001629 return B.getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001630}
1631
Ted Kremenekfa417142009-08-06 01:20:57 +00001632const GRState*
1633RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1634 const GRState *state,
1635 const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001636
Ted Kremenekfa417142009-08-06 01:20:57 +00001637 // Nuke the old bindings stemming from R.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001638 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekfa417142009-08-06 01:20:57 +00001639
Mike Stump11289f42009-09-09 15:08:12 +00001640 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001641 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenekfa417142009-08-06 01:20:57 +00001642
Mike Stump11289f42009-09-09 15:08:12 +00001643 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001644 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001645
Ted Kremenekfa417142009-08-06 01:20:57 +00001646 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1647 // results in a zero-copy algorithm.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001648 return state->makeWithStore(
1649 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenekfa417142009-08-06 01:20:57 +00001650}
Mike Stump11289f42009-09-09 15:08:12 +00001651
Ted Kremenek4533a552009-06-16 22:36:44 +00001652//===----------------------------------------------------------------------===//
1653// State pruning.
1654//===----------------------------------------------------------------------===//
Ted Kremenekcc224242009-09-29 06:35:00 +00001655
Mike Stump11289f42009-09-09 15:08:12 +00001656void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenekcee28a42009-08-02 04:45:08 +00001657 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001658 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001659{
Ted Kremenek9f3a6432009-10-17 17:45:11 +00001660 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1661
Ted Kremenekcee28a42009-08-02 04:45:08 +00001662 Store store = state.getStore();
Ted Kremenek2c85f172009-08-06 04:50:20 +00001663 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001664
Ted Kremenek4533a552009-06-16 22:36:44 +00001665 // The backmap from regions to subregions.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001666 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001667 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekcc224242009-09-29 06:35:00 +00001668
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001669 // Do a pass over the regions in the store. For VarRegions we check if
1670 // the variable is still live and if so add it to the list of live roots.
1671 // For other regions we populate our region backmap.
Ted Kremenek4533a552009-06-16 22:36:44 +00001672 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenekcc224242009-09-29 06:35:00 +00001673
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001674 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001675 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001676 const MemRegion *R = I.getKey();
1677 IntermediateRoots.push_back(R);
Ted Kremenek4533a552009-06-16 22:36:44 +00001678 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001679
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001680 // Process the "intermediate" roots to find if they are referenced by
Mike Stump11289f42009-09-09 15:08:12 +00001681 // real roots.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001682 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001683 llvm::SmallVector<RBDNode, 10> Postponed;
1684
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001685 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001686
Ted Kremenek4533a552009-06-16 22:36:44 +00001687 while (!IntermediateRoots.empty()) {
1688 const MemRegion* R = IntermediateRoots.back();
1689 IntermediateRoots.pop_back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001690
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001691 if (IntermediateVisited.count(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001692 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001693 IntermediateVisited.insert(R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001694
Ted Kremenek4533a552009-06-16 22:36:44 +00001695 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +00001696 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001697 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001698 continue;
1699 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001700
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001701 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001702 llvm::SmallVectorImpl<RBDNode> &Q =
1703 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1704
1705 Q.push_back(std::make_pair(&state, SR));
1706
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001707 continue;
Ted Kremenek4533a552009-06-16 22:36:44 +00001708 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001709
1710 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001711 if (const SubRegion* superR =
Ted Kremenekcc224242009-09-29 06:35:00 +00001712 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001713 IntermediateRoots.push_back(superR);
Ted Kremenek4533a552009-06-16 22:36:44 +00001714 }
Mike Stump11289f42009-09-09 15:08:12 +00001715
Ted Kremenekcc224242009-09-29 06:35:00 +00001716 // Enqueue the RegionRoots onto WorkList.
1717 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1718 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001719 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump11289f42009-09-09 15:08:12 +00001720 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001721 RegionRoots.clear();
1722
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001723 llvm::DenseSet<RBDNode> Visited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001724
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001725tryAgain:
Ted Kremenekcc224242009-09-29 06:35:00 +00001726 while (!WorkList.empty()) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001727 RBDNode N = WorkList.back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001728 WorkList.pop_back();
1729
1730 // Have we visited this node before?
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001731 if (Visited.count(N))
Ted Kremenekcc224242009-09-29 06:35:00 +00001732 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001733 Visited.insert(N);
Mike Stump11289f42009-09-09 15:08:12 +00001734
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001735 const MemRegion *R = N.second;
1736 const GRState *state_N = N.first;
Ted Kremenekcc224242009-09-29 06:35:00 +00001737
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001738 // Enqueue subregions.
1739 RegionStoreSubRegionMap *M;
1740
1741 if (&state == state_N)
1742 M = SubRegions.get();
1743 else {
1744 RegionStoreSubRegionMap *& SM = SC[state_N];
1745 if (!SM)
1746 SM = getRegionStoreSubRegionMap(state_N->getStore());
1747 M = SM;
1748 }
1749
1750 RegionStoreSubRegionMap::iterator I, E;
1751 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1752 WorkList.push_back(std::make_pair(state_N, *I));
1753
Ted Kremenekcc224242009-09-29 06:35:00 +00001754 // Enqueue the super region.
1755 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1756 const MemRegion *superR = SR->getSuperRegion();
1757 if (!isa<MemSpaceRegion>(superR)) {
1758 // If 'R' is a field or an element, we want to keep the bindings
1759 // for the other fields and elements around. The reason is that
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001760 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8b2f5d32009-10-17 07:32:08 +00001761 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1762 || isa<ObjCIvarRegion>(R));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001763 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenekcc224242009-09-29 06:35:00 +00001764 }
1765 }
1766
1767 // Mark the symbol for any live SymbolicRegion as "live". This means we
1768 // should continue to track that symbol.
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001769 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001770 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001771
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001772 // For BlockDataRegions, enqueue the VarRegions for variables marked
1773 // with __block (passed-by-reference).
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001774 // via BlockDeclRefExprs.
1775 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1776 for (BlockDataRegion::referenced_vars_iterator
1777 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001778 RI != RE; ++RI) {
1779 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1780 WorkList.push_back(std::make_pair(state_N, *RI));
1781 }
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001782 // No possible data bindings on a BlockDataRegion. Continue to the
1783 // next region in the worklist.
1784 continue;
1785 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001786
1787 Store store_N = state_N->getStore();
1788 RegionBindings B_N = GetRegionBindings(store_N);
1789
1790 // Get the data binding for R (if any).
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001791 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001792
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001793 if (V) {
1794 // Check for lazy bindings.
1795 if (const nonloc::LazyCompoundVal *LCV =
1796 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001797
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001798 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001799 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001800 }
1801 else {
Ted Kremenekcc224242009-09-29 06:35:00 +00001802 // Update the set of live symbols.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001803 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenekcc224242009-09-29 06:35:00 +00001804 SI!=SE;++SI)
1805 SymReaper.markLive(*SI);
1806
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001807 // If V is a region, then add it to the worklist.
1808 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001809 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenekcc224242009-09-29 06:35:00 +00001810 }
1811 }
1812 }
1813
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001814 // See if any postponed SymbolicRegions are actually live now, after
1815 // having done a scan.
1816 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1817 E = Postponed.end() ; I != E ; ++I) {
1818 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1819 if (SymReaper.isLive(SR->getSymbol())) {
1820 WorkList.push_back(*I);
1821 I->second = NULL;
1822 }
1823 }
1824 }
1825
1826 if (!WorkList.empty())
1827 goto tryAgain;
1828
Ted Kremenek4533a552009-06-16 22:36:44 +00001829 // We have now scanned the store, marking reachable regions and symbols
1830 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00001831 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001832 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001833 const MemRegion* R = I.getKey();
Ted Kremenek4533a552009-06-16 22:36:44 +00001834 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001835 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek4533a552009-06-16 22:36:44 +00001836 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001837
Ted Kremenek4533a552009-06-16 22:36:44 +00001838 // Remove this dead region from the store.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001839 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump11289f42009-09-09 15:08:12 +00001840
Ted Kremenek4533a552009-06-16 22:36:44 +00001841 // Mark all non-live symbols that this region references as dead.
1842 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1843 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00001844
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001845 SVal X = *I.getData().getValue();
Ted Kremenekf106ab92009-08-02 05:00:15 +00001846 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1847 for (; SI != SE; ++SI)
1848 SymReaper.maybeDead(*SI);
1849 }
Mike Stump11289f42009-09-09 15:08:12 +00001850
Ted Kremenekcee28a42009-08-02 04:45:08 +00001851 // Write the store back.
1852 state.setStore(store);
Ted Kremenek4533a552009-06-16 22:36:44 +00001853}
1854
Zhongxing Xudaa41762009-10-13 02:24:55 +00001855GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1856 StackFrameContext const *frame) {
1857 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1858 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1859
1860 FunctionDecl::param_const_iterator PI = FD->param_begin();
1861
1862 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1863
1864 // Copy the arg expression value to the arg variables.
1865 for (; AI != AE; ++AI, ++PI) {
1866 SVal ArgVal = state->getSVal(*AI);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001867 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xudaa41762009-10-13 02:24:55 +00001868 }
1869
1870 return state;
1871}
1872
Ted Kremenek4533a552009-06-16 22:36:44 +00001873//===----------------------------------------------------------------------===//
1874// Utility methods.
1875//===----------------------------------------------------------------------===//
1876
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001877void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00001878 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00001879 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00001880 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00001881
Ted Kremenek2c85f172009-08-06 04:50:20 +00001882 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001883 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00001884}