blob: 4a84eea42f5476d4362e7be1f178d84584440064 [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,
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000290 const CompoundLiteralExpr* CL, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000291
Ted Kremenekb006b822009-11-04 00:09:15 +0000292 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
293 SVal InitVal);
Ted Kremenek609df302009-06-17 22:02:04 +0000294
Ted Kremenekb006b822009-11-04 00:09:15 +0000295 const GRState *BindDeclWithNoInit(const GRState *state,
296 const VarRegion *) {
Ted Kremenek609df302009-06-17 22:02:04 +0000297 return state;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000298 }
Zhongxing Xu83aff702008-10-21 05:29:26 +0000299
Ted Kremenek609df302009-06-17 22:02:04 +0000300 /// BindStruct - Bind a compound value to a structure.
301 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremenek609df302009-06-17 22:02:04 +0000303 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump11289f42009-09-09 15:08:12 +0000304
305 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000306 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000307
Ted Kremenek609df302009-06-17 22:02:04 +0000308 Store Remove(Store store, Loc LV);
309
310 //===------------------------------------------------------------------===//
311 // Loading values from regions.
312 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000313
Ted Kremenek609df302009-06-17 22:02:04 +0000314 /// The high level logic for this method is this:
315 /// Retrieve (L)
316 /// if L has binding
317 /// return L's binding
318 /// else if L is in killset
319 /// return unknown
320 /// else
321 /// if L is on stack or heap
322 /// return undefined
323 /// else
324 /// return symbolic
Ted Kremenekac7c7242009-07-21 21:03:30 +0000325 SValuator::CastResult Retrieve(const GRState *state, Loc L,
326 QualType T = QualType());
Zhongxing Xue67ea5c2009-06-25 04:50:44 +0000327
Ted Kremenek48029552009-07-15 06:09:28 +0000328 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xu2d160732009-06-25 05:29:39 +0000329
Ted Kremenek48029552009-07-15 06:09:28 +0000330 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek48029552009-07-15 06:09:28 +0000332 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenekfe12f882009-07-21 00:12:07 +0000334 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenek834e2f62009-07-20 22:58:02 +0000336 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek040e3b92009-08-06 22:33:36 +0000338 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
339 QualType Ty, const MemRegion *superR);
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek609df302009-06-17 22:02:04 +0000341 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump11289f42009-09-09 15:08:12 +0000342 /// struct copy:
343 /// struct s x, y;
Ted Kremenek609df302009-06-17 22:02:04 +0000344 /// x = y;
345 /// y's value is retrieved by this method.
346 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000347
Ted Kremenek609df302009-06-17 22:02:04 +0000348 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump11289f42009-09-09 15:08:12 +0000349
Ted Kremenekfa417142009-08-06 01:20:57 +0000350 std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +0000351 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump11289f42009-09-09 15:08:12 +0000352
Ted Kremenekfa417142009-08-06 01:20:57 +0000353 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
354 const GRState *state,
355 const TypedRegion *R);
Ted Kremenek609df302009-06-17 22:02:04 +0000356
Ted Kremenek267e45a2009-09-24 04:11:44 +0000357 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
358 QualType T);
359
Ted Kremenek609df302009-06-17 22:02:04 +0000360 //===------------------------------------------------------------------===//
361 // State pruning.
362 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000363
Ted Kremenek609df302009-06-17 22:02:04 +0000364 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
365 /// It returns a new Store with these values removed.
Ted Kremenekcee28a42009-08-02 04:45:08 +0000366 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek609df302009-06-17 22:02:04 +0000367 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
368
Zhongxing Xudaa41762009-10-13 02:24:55 +0000369 const GRState *EnterStackFrame(const GRState *state,
370 const StackFrameContext *frame);
371
Ted Kremenek609df302009-06-17 22:02:04 +0000372 //===------------------------------------------------------------------===//
373 // Region "extents".
374 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek609df302009-06-17 22:02:04 +0000376 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xu383c2732009-11-12 02:48:32 +0000377 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
378 const MemRegion* R);
Ted Kremenek609df302009-06-17 22:02:04 +0000379
380 //===------------------------------------------------------------------===//
Ted Kremenek609df302009-06-17 22:02:04 +0000381 // Utility methods.
382 //===------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenek2c85f172009-08-06 04:50:20 +0000384 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000385 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000386 }
Zhongxing Xucebb7412008-10-24 01:38:55 +0000387
Ted Kremenek799bb6e2009-06-24 23:06:47 +0000388 void print(Store store, llvm::raw_ostream& Out, const char* nl,
389 const char *sep);
Zhongxing Xucebb7412008-10-24 01:38:55 +0000390
391 void iterBindings(Store store, BindingsHandler& f) {
392 // FIXME: Implement.
393 }
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000394
Ted Kremenek609df302009-06-17 22:02:04 +0000395 // FIXME: Remove.
396 BasicValueFactory& getBasicVals() {
397 return StateMgr.getBasicVals();
398 }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenek609df302009-06-17 22:02:04 +0000400 // FIXME: Remove.
Zhongxing Xu6c0d5882008-10-31 07:16:08 +0000401 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xud9959ae2008-10-08 02:50:44 +0000402};
403
404} // end anonymous namespace
405
Ted Kremenek4533a552009-06-16 22:36:44 +0000406//===----------------------------------------------------------------------===//
407// RegionStore creation.
408//===----------------------------------------------------------------------===//
409
410StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
411 RegionStoreFeatures F = maximal_features_tag();
412 return new RegionStoreManager(StMgr, F);
413}
414
415StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
416 RegionStoreFeatures F = minimal_features_tag();
417 F.enableFields(true);
418 return new RegionStoreManager(StMgr, F);
Ted Kremenek6779f892008-10-24 01:04:59 +0000419}
420
Ted Kremenekfa417142009-08-06 01:20:57 +0000421void
422RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump11289f42009-09-09 15:08:12 +0000423 const SubRegion *R) {
Ted Kremenekfa417142009-08-06 01:20:57 +0000424 const MemRegion *superR = R->getSuperRegion();
425 if (add(superR, R))
426 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump11289f42009-09-09 15:08:12 +0000427 WL.push_back(sr);
Ted Kremenekfa417142009-08-06 01:20:57 +0000428}
429
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000430RegionStoreSubRegionMap*
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000431RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
432 RegionBindings B = GetRegionBindings(store);
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000433 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000435 llvm::SmallVector<const SubRegion*, 10> WL;
436
Ted Kremenek2c85f172009-08-06 04:50:20 +0000437 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremenekfa417142009-08-06 01:20:57 +0000438 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
439 M->process(WL, R);
Mike Stump11289f42009-09-09 15:08:12 +0000440
Mike Stump11289f42009-09-09 15:08:12 +0000441 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000442 // don't have direct bindings but are super regions of those that do.
443 while (!WL.empty()) {
444 const SubRegion *R = WL.back();
445 WL.pop_back();
Ted Kremenekfa417142009-08-06 01:20:57 +0000446 M->process(WL, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000447 }
448
Ted Kremenek9f276d62009-03-03 19:02:42 +0000449 return M;
Ted Kremenek8dc671c2009-03-03 01:35:36 +0000450}
Ted Kremenek2907ab72008-12-24 07:46:32 +0000451
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000452SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000453 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000454}
455
Ted Kremenek4533a552009-06-16 22:36:44 +0000456//===----------------------------------------------------------------------===//
Ted Kremenekbca70672009-07-29 18:16:25 +0000457// Binding invalidation.
458//===----------------------------------------------------------------------===//
459
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000460void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
461 const MemRegion *R,
462 RegionStoreSubRegionMap &M) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000463 RegionStoreSubRegionMap::iterator I, E;
464
465 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000466 RemoveSubRegionBindings(B, *I, M);
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenekfa417142009-08-06 01:20:57 +0000468 B = RBFactory.Remove(B, R);
Ted Kremenek1f22aa72009-08-01 06:17:29 +0000469}
470
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000471const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
472 const MemRegion * const *I,
473 const MemRegion * const *E,
474 const Expr *Ex,
475 unsigned Count,
476 InvalidatedSymbols *IS) {
Ted Kremenekbca70672009-07-29 18:16:25 +0000477 ASTContext& Ctx = StateMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +0000478
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000479 // Get the mapping of regions -> subregions.
480 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000481 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000482
483 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000484
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000485 llvm::DenseMap<const MemRegion *, unsigned> Visited;
486 llvm::SmallVector<const MemRegion *, 10> WorkList;
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000487
488 for ( ; I != E; ++I) {
489 // Strip away casts.
490 WorkList.push_back((*I)->StripCasts());
491 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000492
493 while (!WorkList.empty()) {
Ted Kremeneke5716cba2009-12-03 03:27:11 +0000494 const MemRegion *R = WorkList.back();
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000495 WorkList.pop_back();
496
497 // Have we visited this region before?
498 unsigned &visited = Visited[R];
499 if (visited)
500 continue;
501 visited = 1;
Mike Stump11289f42009-09-09 15:08:12 +0000502
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000503 // Add subregions to work list.
504 RegionStoreSubRegionMap::iterator I, E;
505 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
506 WorkList.push_back(*I);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000507
508 // Get the old binding. Is it a region? If so, add it to the worklist.
509 if (Optional<SVal> V = getDirectBinding(B, R)) {
510 if (const MemRegion *RV = V->getAsRegion())
511 WorkList.push_back(RV);
Ted Kremenek1eb68092009-10-16 00:30:49 +0000512
513 // A symbol? Mark it touched by the invalidation.
514 if (IS) {
515 if (SymbolRef Sym = V->getAsSymbol())
516 IS->insert(Sym);
517 }
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000518 }
519
Ted Kremenek1eb68092009-10-16 00:30:49 +0000520 // Symbolic region? Mark that symbol touched by the invalidation.
521 if (IS) {
522 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
523 IS->insert(SR->getSymbol());
524 }
Ted Kremenek5bee5c42009-12-03 08:25:47 +0000525
526 // BlockDataRegion? If so, invalidate captured variables that are passed
527 // by reference.
528 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
529 for (BlockDataRegion::referenced_vars_iterator
530 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
531 I != E; ++I) {
532 const VarRegion *VR = *I;
533 if (VR->getDecl()->getAttr<BlocksAttr>())
534 WorkList.push_back(VR);
535 }
536 continue;
537 }
Ted Kremenek1eb68092009-10-16 00:30:49 +0000538
539 // Handle the region itself.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000540 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
541 isa<ObjCObjectRegion>(R)) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000542 // Invalidate the region by setting its default value to
543 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000544 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
545 Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000546 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000547 continue;
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000550 if (!R->isBoundable())
551 continue;
552
553 const TypedRegion *TR = cast<TypedRegion>(R);
554 QualType T = TR->getValueType(Ctx);
555
556 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000557 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
558
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000559 // No record definition. There is nothing we can do.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000560 if (!RD)
561 continue;
562
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000563 // Invalidate the region by setting its default value to
564 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000565 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
566 Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000567 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000568 continue;
569 }
570
571 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
572 // Set the default value of the array to conjured symbol.
573 DefinedOrUnknownSVal V =
574 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000575 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000576 continue;
577 }
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000578
579 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
580 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000581 // For fields and elements whose super region has also been invalidated,
582 // only remove the old binding. The super region will get set with a
583 // default value from which we can lazily derive a new symbolic value.
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000584 B = RBFactory.Remove(B, R);
585 continue;
586 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000587
Ted Kremenek1cbdf6e2009-09-29 03:12:50 +0000588 // Invalidate the binding.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000589 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
590 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000591 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremenekfa417142009-08-06 01:20:57 +0000592 }
593
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000594 // Create a new state with the updated bindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000595 return state->makeWithStore(B.getRoot());
Ted Kremenekbca70672009-07-29 18:16:25 +0000596}
597
598//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000599// getLValueXXX methods.
600//===----------------------------------------------------------------------===//
601
Ted Kremenek2907ab72008-12-24 07:46:32 +0000602/// getLValueString - Returns an SVal representing the lvalue of a
603/// StringLiteral. Within RegionStore a StringLiteral has an
604/// associated StringRegion, and the lvalue of a StringLiteral is the
605/// lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000606SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000607 return loc::MemRegionVal(MRMgr.getStringRegion(S));
608}
609
Ted Kremenek2907ab72008-12-24 07:46:32 +0000610/// getLValueVar - Returns an SVal that represents the lvalue of a
611/// variable. Within RegionStore a variable has an associated
612/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000613SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000614 const LocationContext *LC) {
615 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000616}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000617
Ted Kremenek2907ab72008-12-24 07:46:32 +0000618/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
619/// of a compound literal. Within RegionStore a compound literal
620/// has an associated region, and the lvalue of the compound literal
621/// is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000622SVal
623RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000624 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
625}
626
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000627SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
628 return getLValueFieldOrIvar(D, Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000629}
630
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000631SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
632 return getLValueFieldOrIvar(D, Base);
Ted Kremenekd3c82762009-03-05 04:50:08 +0000633}
634
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000635SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000636 if (Base.isUnknownOrUndef())
637 return Base;
638
639 Loc BaseL = cast<Loc>(Base);
640 const MemRegion* BaseR = 0;
641
642 switch (BaseL.getSubKind()) {
643 case loc::MemRegionKind:
644 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
645 break;
646
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000647 case loc::GotoLabelKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000648 // These are anormal cases. Flag an undefined value.
649 return UndefinedVal();
650
651 case loc::ConcreteIntKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000652 // While these seem funny, this can happen through casts.
653 // FIXME: What we should return is the field offset. For example,
654 // add the field offset to the integer value. That way funny things
655 // like this work properly: &(((struct foo *) 0xa)->f)
656 return Base;
657
658 default:
Zhongxing Xue79a4e62008-11-07 08:57:30 +0000659 assert(0 && "Unhandled Base.");
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000660 return Base;
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenekd3c82762009-03-05 04:50:08 +0000663 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
664 // of FieldDecl.
665 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
666 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000667
Ted Kremenekd3c82762009-03-05 04:50:08 +0000668 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000669}
670
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000671SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
672 SVal Base) {
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000673
Ted Kremenek06032222009-03-09 22:44:49 +0000674 // If the base is an unknown or undefined value, just return it back.
675 // FIXME: For absolute pointer addresses, we just return that value back as
676 // well, although in reality we should return the offset added to that
677 // value.
678 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu36d4ade2008-10-27 12:23:17 +0000679 return Base;
680
Ted Kremenek92d48a72009-01-22 20:27:48 +0000681 // Only handle integer offsets... for now.
682 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000683 return UnknownVal();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000684
Zhongxing Xu7c382642009-05-09 13:20:07 +0000685 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000686
687 // Pointer of any type can be cast and used as array base.
688 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000690 // Convert the offset to the appropriate size and signedness.
691 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000692
Ted Kremenek92d48a72009-01-22 20:27:48 +0000693 if (!ElemR) {
694 //
695 // If the base region is not an ElementRegion, create one.
696 // This can happen in the following example:
697 //
698 // char *p = __builtin_alloc(10);
699 // p[1] = 8;
700 //
Zhongxing Xu7c382642009-05-09 13:20:07 +0000701 // Observe that 'p' binds to an AllocaRegion.
Ted Kremenek92d48a72009-01-22 20:27:48 +0000702 //
Ted Kremenek02e50892009-05-04 06:18:28 +0000703 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000704 BaseRegion, getContext()));
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000705 }
Mike Stump11289f42009-09-09 15:08:12 +0000706
Ted Kremenek92d48a72009-01-22 20:27:48 +0000707 SVal BaseIdx = ElemR->getIndex();
Mike Stump11289f42009-09-09 15:08:12 +0000708
Ted Kremenek92d48a72009-01-22 20:27:48 +0000709 if (!isa<nonloc::ConcreteInt>(BaseIdx))
710 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000711
Ted Kremenek92d48a72009-01-22 20:27:48 +0000712 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
713 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
714 assert(BaseIdxI.isSigned());
Mike Stump11289f42009-09-09 15:08:12 +0000715
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000716 // Compute the new index.
717 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump11289f42009-09-09 15:08:12 +0000718
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000719 // Construct the new ElementRegion.
720 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000721 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump11289f42009-09-09 15:08:12 +0000722 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000723}
724
Ted Kremenek4533a552009-06-16 22:36:44 +0000725//===----------------------------------------------------------------------===//
726// Extents for regions.
727//===----------------------------------------------------------------------===//
728
Zhongxing Xu383c2732009-11-12 02:48:32 +0000729DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
730 const MemRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +0000731
Ted Kremenek94575aa2009-07-10 22:30:06 +0000732 switch (R->getKind()) {
733 case MemRegion::MemSpaceRegionKind:
734 assert(0 && "Cannot index into a MemSpace");
Mike Stump11289f42009-09-09 15:08:12 +0000735 return UnknownVal();
736
Ted Kremenek10a50e72009-11-25 01:32:22 +0000737 case MemRegion::FunctionTextRegionKind:
738 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000739 case MemRegion::BlockDataRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000740 // Technically this can happen if people do funny things with casts.
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000741 return UnknownVal();
Ted Kremenek94575aa2009-07-10 22:30:06 +0000742
743 // Not yet handled.
744 case MemRegion::AllocaRegionKind:
745 case MemRegion::CompoundLiteralRegionKind:
746 case MemRegion::ElementRegionKind:
747 case MemRegion::FieldRegionKind:
748 case MemRegion::ObjCIvarRegionKind:
749 case MemRegion::ObjCObjectRegionKind:
750 case MemRegion::SymbolicRegionKind:
751 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000752
Ted Kremenek94575aa2009-07-10 22:30:06 +0000753 case MemRegion::StringRegionKind: {
754 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000755 // We intentionally made the size value signed because it participates in
Ted Kremenek94575aa2009-07-10 22:30:06 +0000756 // operations with signed indices.
757 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Ted Kremenek94575aa2009-07-10 22:30:06 +0000760 case MemRegion::VarRegionKind: {
761 const VarRegion* VR = cast<VarRegion>(R);
762 // Get the type of the variable.
763 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000764
Ted Kremenek94575aa2009-07-10 22:30:06 +0000765 // FIXME: Handle variable-length arrays.
766 if (isa<VariableArrayType>(T))
767 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000768
Ted Kremenek94575aa2009-07-10 22:30:06 +0000769 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
770 // return the size as signed integer.
771 return ValMgr.makeIntVal(CAT->getSize(), false);
772 }
Ted Kremenekca7935d2009-08-02 05:15:23 +0000773
Ted Kremenek94575aa2009-07-10 22:30:06 +0000774 // Clients can use ordinary variables as if they were arrays. These
775 // essentially are arrays of size 1.
776 return ValMgr.makeIntVal(1, false);
Zhongxing Xuea8c48d2009-05-06 11:51:48 +0000777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Ted Kremenek94575aa2009-07-10 22:30:06 +0000779 case MemRegion::BEG_DECL_REGIONS:
780 case MemRegion::END_DECL_REGIONS:
781 case MemRegion::BEG_TYPED_REGIONS:
782 case MemRegion::END_TYPED_REGIONS:
783 assert(0 && "Infeasible region");
784 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000785 }
Mike Stump11289f42009-09-09 15:08:12 +0000786
Ted Kremenek94575aa2009-07-10 22:30:06 +0000787 assert(0 && "Unreachable");
Ted Kremenek47ad37d2009-01-06 19:12:06 +0000788 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000789}
790
Ted Kremenek609df302009-06-17 22:02:04 +0000791const GRState *RegionStoreManager::setExtent(const GRState *state,
792 const MemRegion *region,
793 SVal extent) {
794 return state->set<RegionExtents>(region, extent);
Ted Kremenek4533a552009-06-16 22:36:44 +0000795}
796
797//===----------------------------------------------------------------------===//
798// Location and region casting.
799//===----------------------------------------------------------------------===//
800
Ted Kremenek2907ab72008-12-24 07:46:32 +0000801/// ArrayToPointer - Emulates the "decay" of an array to a pointer
802/// type. 'Array' represents the lvalue of the array being decayed
803/// to a pointer, and the returned SVal represents the decayed
804/// version of that lvalue (i.e., a pointer to the first element of
805/// the array). This is called by GRExprEngine when evaluating casts
806/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000807SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000808 if (!isa<loc::MemRegionVal>(Array))
809 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000810
Ted Kremenekf065b152008-12-13 19:24:37 +0000811 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
812 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000813
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000814 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000815 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000816
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000817 // Strip off typedefs from the ArrayRegion's ValueType.
John McCalla1925362009-09-29 23:03:30 +0000818 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000819 ArrayType *AT = cast<ArrayType>(T);
820 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000821
Ted Kremenekccc22922009-07-16 00:00:11 +0000822 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenek721fcc02009-12-04 00:26:31 +0000823 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
824 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000825}
826
Ted Kremenek4533a552009-06-16 22:36:44 +0000827//===----------------------------------------------------------------------===//
828// Pointer arithmetic.
829//===----------------------------------------------------------------------===//
830
Mike Stump11289f42009-09-09 15:08:12 +0000831SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000832 BinaryOperator::Opcode Op, Loc L, NonLoc R,
833 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000834 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000835 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000836 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000837
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000838 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000839 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000840
Ted Kremenekf6f04612009-07-11 00:58:27 +0000841 switch (MR->getKind()) {
842 case MemRegion::SymbolicRegionKind: {
843 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000844 SymbolRef Sym = SR->getSymbol();
Ted Kremenekd1d60662009-08-25 22:55:09 +0000845 QualType T = Sym->getType(getContext());
846 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000847
Ted Kremenekd1d60662009-08-25 22:55:09 +0000848 if (const PointerType *PT = T->getAs<PointerType>())
849 EleTy = PT->getPointeeType();
850 else
John McCall9dd450b2009-09-21 23:43:11 +0000851 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000852
Ted Kremenekf6f04612009-07-11 00:58:27 +0000853 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
854 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000855 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000856 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000857 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000858 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000859 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000860 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenekf6f04612009-07-11 00:58:27 +0000861 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
862 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000863 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000864 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000865
Ted Kremenekf6f04612009-07-11 00:58:27 +0000866 case MemRegion::ElementRegionKind: {
867 ER = cast<ElementRegion>(MR);
868 break;
869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenekf6f04612009-07-11 00:58:27 +0000871 // Not yet handled.
872 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000873 case MemRegion::StringRegionKind: {
874
875 }
876 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000877 case MemRegion::CompoundLiteralRegionKind:
878 case MemRegion::FieldRegionKind:
879 case MemRegion::ObjCObjectRegionKind:
880 case MemRegion::ObjCIvarRegionKind:
881 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000882
Ted Kremenek10a50e72009-11-25 01:32:22 +0000883 case MemRegion::FunctionTextRegionKind:
884 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000885 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000886 // Technically this can happen if people do funny things with casts.
887 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenekf6f04612009-07-11 00:58:27 +0000889 case MemRegion::MemSpaceRegionKind:
890 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
891 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000892
Ted Kremenekf6f04612009-07-11 00:58:27 +0000893 case MemRegion::BEG_DECL_REGIONS:
894 case MemRegion::END_DECL_REGIONS:
895 case MemRegion::BEG_TYPED_REGIONS:
896 case MemRegion::END_TYPED_REGIONS:
897 assert(0 && "Infeasible region");
898 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000899 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000900
Zhongxing Xue7d14932009-03-02 07:52:23 +0000901 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000902 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +0000903
Ted Kremenek8ec57712009-10-06 01:39:48 +0000904 // For now, only support:
905 // (a) concrete integer indices that can easily be resolved
906 // (b) 0 + symbolic index
907 if (Base) {
908 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
909 // FIXME: Should use SValuator here.
910 SVal NewIdx =
911 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000912 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +0000913 const MemRegion* NewER =
914 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
915 ER->getSuperRegion(), getContext());
916 return ValMgr.makeLoc(NewER);
917 }
918 if (0 == Base->getValue()) {
919 const MemRegion* NewER =
920 MRMgr.getElementRegion(ER->getElementType(), R,
921 ER->getSuperRegion(), getContext());
922 return ValMgr.makeLoc(NewER);
923 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000926 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000927}
928
Ted Kremenek4533a552009-06-16 22:36:44 +0000929//===----------------------------------------------------------------------===//
930// Loading values from regions.
931//===----------------------------------------------------------------------===//
932
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000933Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
934 const MemRegion *R) {
935 if (const BindingVal *BV = B.lookup(R))
936 return Optional<SVal>::create(BV->getDirectValue());
937
938 return Optional<SVal>();
939}
940
941Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000942 const MemRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +0000943
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000944 if (R->isBoundable())
945 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
946 if (TR->getValueType(getContext())->isUnionType())
947 return UnknownVal();
948
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000949 if (BindingVal const *V = B.lookup(R))
950 return Optional<SVal>::create(V->getDefaultValue());
951
952 return Optional<SVal>();
953}
954
955Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
956 const MemRegion *R) {
957 if (const BindingVal *BV = B.lookup(R))
958 return Optional<SVal>::create(BV->getValue());
959
960 return Optional<SVal>();
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000961}
962
Ted Kremeneke6fea682009-07-15 02:31:43 +0000963static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
964 RTy = Ctx.getCanonicalType(RTy);
965 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Ted Kremeneke6fea682009-07-15 02:31:43 +0000967 if (RTy == UsedTy)
968 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000969
970
Ted Kremenek834e2f62009-07-20 22:58:02 +0000971 // Recursively check the types. We basically want to see if a pointer value
Mike Stump11289f42009-09-09 15:08:12 +0000972 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek834e2f62009-07-20 22:58:02 +0000973 // represents a reinterpretation.
974 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump11289f42009-09-09 15:08:12 +0000975 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000976 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek834e2f62009-07-20 22:58:02 +0000977
978 return PUsedTy && PRTy &&
979 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump11289f42009-09-09 15:08:12 +0000980 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek834e2f62009-07-20 22:58:02 +0000981 }
982
983 return true;
Ted Kremeneke6fea682009-07-15 02:31:43 +0000984}
985
Ted Kremenek267e45a2009-09-24 04:11:44 +0000986const ElementRegion *
987RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
988 ASTContext &Ctx = getContext();
989 SVal idx = ValMgr.makeZeroArrayIndex();
990 assert(!T.isNull());
991 return MRMgr.getElementRegion(T, idx, SR, Ctx);
992}
993
994
995
Ted Kremenekac7c7242009-07-21 21:03:30 +0000996SValuator::CastResult
997RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek609df302009-06-17 22:02:04 +0000998
Zhongxing Xu83aff702008-10-21 05:29:26 +0000999 assert(!isa<UnknownVal>(L) && "location unknown");
1000 assert(!isa<UndefinedVal>(L) && "location undefined");
1001
Ted Kremenek2907ab72008-12-24 07:46:32 +00001002 // FIXME: Is this even possible? Shouldn't this be treated as a null
1003 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001004 if (isa<loc::ConcreteInt>(L))
Ted Kremenekac7c7242009-07-21 21:03:30 +00001005 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu83aff702008-10-21 05:29:26 +00001006
Ted Kremenek609df302009-06-17 22:02:04 +00001007 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001008
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001009 // FIXME: return symbolic value for these cases.
Zhongxing Xuec7e7df2009-04-03 07:33:13 +00001010 // Example:
1011 // void f(int* p) { int x = *p; }
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001012 // char* p = alloca();
1013 // read(p);
1014 // c = *p;
Ted Kremenek0c37d192009-07-14 20:48:22 +00001015 if (isa<AllocaRegion>(MR))
Ted Kremenekac7c7242009-07-21 21:03:30 +00001016 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001017
Ted Kremenek267e45a2009-09-24 04:11:44 +00001018 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1019 MR = GetElementZeroRegion(SR, T);
Mike Stump11289f42009-09-09 15:08:12 +00001020
Ted Kremenek0bb32e32009-08-03 21:41:46 +00001021 if (isa<CodeTextRegion>(MR))
1022 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001023
Ted Kremenek2907ab72008-12-24 07:46:32 +00001024 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1025 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +00001026 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001027 QualType RTy = R->getValueType(getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001028
Ted Kremenek2907ab72008-12-24 07:46:32 +00001029 // FIXME: We should eventually handle funny addressing. e.g.:
1030 //
1031 // int x = ...;
1032 // int *p = &x;
1033 // char *q = (char*) p;
1034 // char c = *q; // returns the first byte of 'x'.
1035 //
1036 // Such funny addressing will occur due to layering of regions.
1037
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001038#if 0
Ted Kremeneke6fea682009-07-15 02:31:43 +00001039 ASTContext &Ctx = getContext();
1040 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001041 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1042 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001043 RTy = T;
Ted Kremenek57fa7e32009-07-15 04:23:32 +00001044 assert(Ctx.getCanonicalType(RTy) ==
1045 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump11289f42009-09-09 15:08:12 +00001046 }
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001047#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001048
Zhongxing Xuce270a62009-03-09 09:15:51 +00001049 if (RTy->isStructureType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001050 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump11289f42009-09-09 15:08:12 +00001051
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001052 // FIXME: Handle unions.
1053 if (RTy->isUnionType())
1054 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001055
1056 if (RTy->isArrayType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001057 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001058
Zhongxing Xuce270a62009-03-09 09:15:51 +00001059 // FIXME: handle Vector types.
1060 if (RTy->isVectorType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001061 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu0628f532009-06-28 14:16:39 +00001062
1063 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001064 return SValuator::CastResult(state,
1065 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu0628f532009-06-28 14:16:39 +00001066
1067 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001068 return SValuator::CastResult(state,
1069 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump11289f42009-09-09 15:08:12 +00001070
Ted Kremenek834e2f62009-07-20 22:58:02 +00001071 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001072 return SValuator::CastResult(state,
1073 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump11289f42009-09-09 15:08:12 +00001074
Ted Kremenekfe12f882009-07-21 00:12:07 +00001075 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001076 return SValuator::CastResult(state,
1077 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek834e2f62009-07-20 22:58:02 +00001078
Ted Kremenek2c85f172009-08-06 04:50:20 +00001079 RegionBindings B = GetRegionBindings(state->getStore());
1080 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001081
1082 // Check if the region has a binding.
1083 if (V)
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001084 if (SVal const *SV = V->getValue())
1085 return SValuator::CastResult(state, *SV);
Ted Kremenek2907ab72008-12-24 07:46:32 +00001086
Ted Kremenek2907ab72008-12-24 07:46:32 +00001087 // The location does not have a bound value. This means that it has
1088 // the value it had upon its creation and/or entry to the analyzed
1089 // function/method. These are either symbolic values or 'undefined'.
1090
Ted Kremenek920ad712009-07-22 04:35:42 +00001091#if HEAP_UNDEFINED
Ted Kremenek2d99f972009-06-23 18:17:08 +00001092 if (R->hasHeapOrStackStorage()) {
Ted Kremenek920ad712009-07-22 04:35:42 +00001093#else
1094 if (R->hasStackStorage()) {
1095#endif
Ted Kremenek2907ab72008-12-24 07:46:32 +00001096 // All stack variables are considered to have undefined values
1097 // upon creation. All heap allocated blocks are considered to
1098 // have undefined values as well unless they are explicitly bound
1099 // to specific values.
Ted Kremenekac7c7242009-07-21 21:03:30 +00001100 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek2907ab72008-12-24 07:46:32 +00001101 }
1102
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001103 // All other values are symbolic.
Ted Kremenekac7c7242009-07-21 21:03:30 +00001104 return SValuator::CastResult(state,
1105 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu83aff702008-10-21 05:29:26 +00001106}
Mike Stump11289f42009-09-09 15:08:12 +00001107
Ted Kremenekfa417142009-08-06 01:20:57 +00001108std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001109RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001110 if (Optional<SVal> OV = getDirectBinding(B, R))
1111 if (const nonloc::LazyCompoundVal *V =
1112 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1113 return std::make_pair(V->getState(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001114
Ted Kremenekfa417142009-08-06 01:20:57 +00001115 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1116 const std::pair<const GRState *, const MemRegion *> &X =
1117 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001118
Ted Kremenekfa417142009-08-06 01:20:57 +00001119 if (X.first)
1120 return std::make_pair(X.first,
1121 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001122 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001123 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1124 const std::pair<const GRState *, const MemRegion *> &X =
1125 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001126
Ted Kremenekfa417142009-08-06 01:20:57 +00001127 if (X.first)
1128 return std::make_pair(X.first,
1129 MRMgr.getFieldRegionWithSuper(FR, X.second));
1130 }
1131
1132 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1133}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001134
Zhongxing Xu2d160732009-06-25 05:29:39 +00001135SVal RegionStoreManager::RetrieveElement(const GRState* state,
1136 const ElementRegion* R) {
1137 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001138 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001139 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001140 return *V;
1141
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001142 const MemRegion* superR = R->getSuperRegion();
1143
Zhongxing Xu2d160732009-06-25 05:29:39 +00001144 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001145 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek228539f2009-09-29 16:36:48 +00001146 // FIXME: Handle loads from strings where the literal is treated as
1147 // an integer, e.g., *((unsigned int*)"hello")
1148 ASTContext &Ctx = getContext();
Douglas Gregor4ef1d402009-11-09 22:08:55 +00001149 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001150 if (T != Ctx.getCanonicalType(R->getElementType()))
1151 return UnknownVal();
1152
Zhongxing Xu2d160732009-06-25 05:29:39 +00001153 const StringLiteral *Str = StrR->getStringLiteral();
1154 SVal Idx = R->getIndex();
1155 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1156 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001157 int64_t byteLength = Str->getByteLength();
Ted Kremenekb5850f92009-09-05 17:59:01 +00001158 if (i > byteLength) {
1159 // Buffer overflow checking in GRExprEngine should handle this case,
1160 // but we shouldn't rely on it to not overflow here if that checking
1161 // is disabled.
1162 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001163 }
Ted Kremenekb5850f92009-09-05 17:59:01 +00001164 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001165 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001166 }
1167 }
Mike Stump11289f42009-09-09 15:08:12 +00001168
Ted Kremenek040e3b92009-08-06 22:33:36 +00001169 // Check if the immediate super region has a direct binding.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001170 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneke6fea682009-07-15 02:31:43 +00001171 if (SymbolRef parentSym = V->getAsSymbol())
1172 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek920ad712009-07-22 04:35:42 +00001173
1174 if (V->isUnknownOrUndef())
1175 return *V;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001176
1177 // Handle LazyCompoundVals for the immediate super region. Other cases
1178 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump11289f42009-09-09 15:08:12 +00001179 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek040e3b92009-08-06 22:33:36 +00001180 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump11289f42009-09-09 15:08:12 +00001181
Ted Kremenek040e3b92009-08-06 22:33:36 +00001182 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1183 return RetrieveElement(LCV->getState(), R);
1184 }
Mike Stump11289f42009-09-09 15:08:12 +00001185
Ted Kremeneke6fea682009-07-15 02:31:43 +00001186 // Other cases: give up.
Zhongxing Xu61e66922009-07-03 06:11:41 +00001187 return UnknownVal();
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001188 }
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001189
Ted Kremenek040e3b92009-08-06 22:33:36 +00001190 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001191}
1192
Mike Stump11289f42009-09-09 15:08:12 +00001193SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001194 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001195
1196 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001197 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001198 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001199 return *V;
1200
Ted Kremenek040e3b92009-08-06 22:33:36 +00001201 QualType Ty = R->getValueType(getContext());
1202 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1203}
Mike Stump11289f42009-09-09 15:08:12 +00001204
Ted Kremenek040e3b92009-08-06 22:33:36 +00001205SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1206 const TypedRegion *R,
1207 QualType Ty,
1208 const MemRegion *superR) {
1209
Mike Stump11289f42009-09-09 15:08:12 +00001210 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001211 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001212
Ted Kremenek040e3b92009-08-06 22:33:36 +00001213 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001214
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001215 while (superR) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001216 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001217 if (SymbolRef parentSym = D->getAsSymbol())
1218 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001219
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001220 if (D->isZeroConstant())
1221 return ValMgr.makeZeroVal(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001222
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001223 if (D->isUnknown())
1224 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001225
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001226 assert(0 && "Unknown default value");
1227 }
Mike Stump11289f42009-09-09 15:08:12 +00001228
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001229 // If our super region is a field or element itself, walk up the region
1230 // hierarchy to see if there is a default value installed in an ancestor.
1231 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1232 superR = cast<SubRegion>(superR)->getSuperRegion();
1233 continue;
1234 }
Mike Stump11289f42009-09-09 15:08:12 +00001235
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001236 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001237 }
Mike Stump11289f42009-09-09 15:08:12 +00001238
Ted Kremenekfa417142009-08-06 01:20:57 +00001239 // Lazy binding?
1240 const GRState *lazyBindingState = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001241 const MemRegion *lazyBindingRegion = NULL;
1242 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001243
Ted Kremenekfa417142009-08-06 01:20:57 +00001244 if (lazyBindingState) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001245 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump11289f42009-09-09 15:08:12 +00001246
Ted Kremenek040e3b92009-08-06 22:33:36 +00001247 if (isa<ElementRegion>(R))
1248 return RetrieveElement(lazyBindingState,
1249 cast<ElementRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001250
Ted Kremenekfa417142009-08-06 01:20:57 +00001251 return RetrieveField(lazyBindingState,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001252 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001253 }
1254
Ted Kremenek040e3b92009-08-06 22:33:36 +00001255 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump11289f42009-09-09 15:08:12 +00001256
Ted Kremenek040e3b92009-08-06 22:33:36 +00001257 if (isa<ElementRegion>(R)) {
1258 // Currently we don't reason specially about Clang-style vectors. Check
1259 // if superR is a vector and if so return Unknown.
1260 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1261 if (typedSuperR->getValueType(getContext())->isVectorType())
1262 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001263 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001264 }
Mike Stump11289f42009-09-09 15:08:12 +00001265
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001266 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001267 }
Mike Stump11289f42009-09-09 15:08:12 +00001268
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001269 // All other values are symbolic.
1270 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001271}
Mike Stump11289f42009-09-09 15:08:12 +00001272
1273SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek48029552009-07-15 06:09:28 +00001274 const ObjCIvarRegion* R) {
1275
Ted Kremenek48029552009-07-15 06:09:28 +00001276 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001277 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek48029552009-07-15 06:09:28 +00001278
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001279 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001280 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001281
Ted Kremenek48029552009-07-15 06:09:28 +00001282 const MemRegion *superR = R->getSuperRegion();
1283
Ted Kremenek481c1212009-10-20 01:20:57 +00001284 // Check if the super region has a default binding.
1285 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001286 if (SymbolRef parentSym = V->getAsSymbol())
1287 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001288
Ted Kremenek48029552009-07-15 06:09:28 +00001289 // Other cases: give up.
1290 return UnknownVal();
1291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Ted Kremenek834e2f62009-07-20 22:58:02 +00001293 return RetrieveLazySymbol(state, R);
1294}
1295
Ted Kremenekfe12f882009-07-21 00:12:07 +00001296SVal RegionStoreManager::RetrieveVar(const GRState *state,
1297 const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001298
Ted Kremenekfe12f882009-07-21 00:12:07 +00001299 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001300 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001301
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001302 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001303 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001304
Ted Kremenekfe12f882009-07-21 00:12:07 +00001305 // Lazily derive a value for the VarRegion.
1306 const VarDecl *VD = R->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001307
Ted Kremenekfe12f882009-07-21 00:12:07 +00001308 if (R->hasGlobalsOrParametersStorage())
1309 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001310
Ted Kremenekfe12f882009-07-21 00:12:07 +00001311 return UndefinedVal();
1312}
1313
Mike Stump11289f42009-09-09 15:08:12 +00001314SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek834e2f62009-07-20 22:58:02 +00001315 const TypedRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001316
Ted Kremenek834e2f62009-07-20 22:58:02 +00001317 QualType valTy = R->getValueType(getContext());
Ted Kremenek920ad712009-07-22 04:35:42 +00001318
Ted Kremenek48029552009-07-15 06:09:28 +00001319 // All other values are symbolic.
Ted Kremenek834e2f62009-07-20 22:58:02 +00001320 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek48029552009-07-15 06:09:28 +00001321}
1322
Mike Stump11289f42009-09-09 15:08:12 +00001323SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1324 const TypedRegion* R) {
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001325 QualType T = R->getValueType(getContext());
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001326 assert(T->isStructureType());
1327
Zhongxing Xud85a9912009-06-11 07:27:30 +00001328 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001329 RecordDecl* RD = RT->getDecl();
1330 assert(RD->isDefinition());
Mike Stumpc700b362009-08-06 12:56:50 +00001331 (void)RD;
Ted Kremenekfa417142009-08-06 01:20:57 +00001332#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001333 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1334
Ted Kremenek609df302009-06-17 22:02:04 +00001335 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1336 // reverse iterator, we should implement one.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001337 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001338
Douglas Gregor7a4fad12008-12-11 20:41:00 +00001339 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1340 FieldEnd = Fields.rend();
1341 Field != FieldEnd; ++Field) {
1342 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001343 QualType FTy = (*Field)->getType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001344 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001345 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1346 }
1347
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001348 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001349#else
1350 return ValMgr.makeLazyCompoundVal(state, R);
1351#endif
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001352}
1353
Ted Kremenek609df302009-06-17 22:02:04 +00001354SVal RegionStoreManager::RetrieveArray(const GRState *state,
1355 const TypedRegion * R) {
Ted Kremenekfa417142009-08-06 01:20:57 +00001356#if USE_EXPLICIT_COMPOUND
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001357 QualType T = R->getValueType(getContext());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001358 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1359
1360 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001361 uint64_t size = CAT->getSize().getZExtValue();
1362 for (uint64_t i = 0; i < size; ++i) {
1363 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu838a0db2009-06-16 09:55:50 +00001364 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump11289f42009-09-09 15:08:12 +00001365 getContext());
Ted Kremenek02e50892009-05-04 06:18:28 +00001366 QualType ETy = ER->getElementType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001367 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001368 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1369 }
1370
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001371 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001372#else
1373 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1374 return ValMgr.makeLazyCompoundVal(state, R);
1375#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001376}
1377
Ted Kremenek4533a552009-06-16 22:36:44 +00001378//===----------------------------------------------------------------------===//
1379// Binding values to regions.
1380//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001381
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001382Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001383 const MemRegion* R = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001384
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001385 if (isa<loc::MemRegionVal>(L))
1386 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001387
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001388 if (R) {
Mike Stump11289f42009-09-09 15:08:12 +00001389 RegionBindings B = GetRegionBindings(store);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001390 return RBFactory.Remove(B, R).getRoot();
1391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001393 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001394}
1395
Ted Kremenek609df302009-06-17 22:02:04 +00001396const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001397 if (isa<loc::ConcreteInt>(L))
1398 return state;
1399
Ted Kremenek4533a552009-06-16 22:36:44 +00001400 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001401 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001402
Ted Kremenek4533a552009-06-16 22:36:44 +00001403 // Check if the region is a struct region.
1404 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1405 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001406 return BindStruct(state, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001407
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001408 // Special case: the current region represents a cast and it and the super
1409 // region both have pointer types or intptr_t types. If so, perform the
1410 // bind to the super region.
1411 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump11289f42009-09-09 15:08:12 +00001412 // loads that treat integers as pointers and vis versa.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001413 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1414 if (ER->getIndex().isZeroConstant()) {
1415 if (const TypedRegion *superR =
1416 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1417 ASTContext &Ctx = getContext();
1418 QualType superTy = superR->getValueType(Ctx);
1419 QualType erTy = ER->getValueType(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001420
1421 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001422 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump11289f42009-09-09 15:08:12 +00001423 SValuator::CastResult cr =
1424 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001425 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1426 }
Ted Kremenek25c9c142009-09-21 22:58:52 +00001427 // For now, just invalidate the fields of the struct/union/class.
1428 // FIXME: Precisely handle the fields of the record.
1429 if (superTy->isRecordType())
Ted Kremenek1eb68092009-10-16 00:30:49 +00001430 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001431 }
1432 }
1433 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001434 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1435 // Binding directly to a symbolic region should be treated as binding
1436 // to element 0.
1437 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek1b40e592009-09-24 06:24:32 +00001438 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek267e45a2009-09-24 04:11:44 +00001439 R = GetElementZeroRegion(SR, T);
1440 }
Mike Stump11289f42009-09-09 15:08:12 +00001441
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001442 // Perform the binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001443 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001444 return state->makeWithStore(
1445 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek4533a552009-06-16 22:36:44 +00001446}
1447
Ted Kremenek14536f62009-08-21 22:28:32 +00001448const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekb006b822009-11-04 00:09:15 +00001449 const VarRegion *VR,
Ted Kremenek14536f62009-08-21 22:28:32 +00001450 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001451
Ted Kremenekb006b822009-11-04 00:09:15 +00001452 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001453
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001454 if (T->isArrayType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001455 return BindArray(ST, VR, InitVal);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001456 if (T->isStructureType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001457 return BindStruct(ST, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001458
Ted Kremenek14536f62009-08-21 22:28:32 +00001459 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001460}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001461
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001462// FIXME: this method should be merged into Bind().
Ted Kremenek609df302009-06-17 22:02:04 +00001463const GRState *
1464RegionStoreManager::BindCompoundLiteral(const GRState *state,
1465 const CompoundLiteralExpr* CL,
1466 SVal V) {
Ted Kremenek721fcc02009-12-04 00:26:31 +00001467 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)), V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001468}
1469
Ted Kremenek439a6d12009-11-19 20:20:24 +00001470const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1471 const MemRegion *R,
1472 QualType T) {
1473 Store store = state->getStore();
1474 RegionBindings B = GetRegionBindings(store);
1475 SVal V;
1476
1477 if (Loc::IsLocType(T))
1478 V = ValMgr.makeNull();
1479 else if (T->isIntegerType())
1480 V = ValMgr.makeZeroVal(T);
1481 else if (T->isStructureType() || T->isArrayType()) {
1482 // Set the default value to a zero constant when it is a structure
1483 // or array. The type doesn't really matter.
1484 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1485 }
1486 else {
1487 return state;
1488 }
1489
1490 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1491 return state->makeWithStore(B.getRoot());
1492}
1493
Ted Kremenek609df302009-06-17 22:02:04 +00001494const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001495 const TypedRegion* R,
Ted Kremenek609df302009-06-17 22:02:04 +00001496 SVal Init) {
1497
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001498 QualType T = R->getValueType(getContext());
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001499 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001500 QualType ElementTy = CAT->getElementType();
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001501
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001502 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001503
1504 // Check if the init expr is a StringLiteral.
1505 if (isa<loc::MemRegionVal>(Init)) {
1506 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1507 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1508 const char* str = S->getStrData();
1509 unsigned len = S->getByteLength();
1510 unsigned j = 0;
1511
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001512 // Copy bytes from the string literal into the target array. Trailing bytes
1513 // in the array that are not covered by the string literal are initialized
1514 // to zero.
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001515 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001516 if (j >= len)
1517 break;
1518
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001519 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001520 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1521 getContext());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001522
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001523 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek609df302009-06-17 22:02:04 +00001524 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001525 }
1526
Ted Kremenek609df302009-06-17 22:02:04 +00001527 return state;
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001528 }
1529
Ted Kremenekfa417142009-08-06 01:20:57 +00001530 // Handle lazy compound values.
1531 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1532 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001533
1534 // Remaining case: explicit compound values.
Ted Kremenek439a6d12009-11-19 20:20:24 +00001535
1536 if (Init.isUnknown())
1537 return setImplicitDefaultValue(state, R, ElementTy);
1538
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001539 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001540 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001541 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001542
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001543 for (; i < size; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001544 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001545 if (VI == VE)
1546 break;
1547
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001548 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001549 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001550
1551 if (CAT->getElementType()->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001552 state = BindStruct(state, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001553 else
Ted Kremenek30030012009-09-22 21:19:14 +00001554 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001555 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001556 }
1557
Ted Kremenek439a6d12009-11-19 20:20:24 +00001558 // If the init list is shorter than the array length, set the
1559 // array default value.
1560 if (i < size)
1561 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001562
Ted Kremenek609df302009-06-17 22:02:04 +00001563 return state;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001564}
1565
Ted Kremenek609df302009-06-17 22:02:04 +00001566const GRState *
1567RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1568 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001569
Ted Kremenek609df302009-06-17 22:02:04 +00001570 if (!Features.supportsFields())
1571 return state;
Mike Stump11289f42009-09-09 15:08:12 +00001572
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001573 QualType T = R->getValueType(getContext());
Zhongxing Xub393b502008-10-31 10:53:01 +00001574 assert(T->isStructureType());
1575
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001576 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001577 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001578
1579 if (!RD->isDefinition())
Ted Kremenek609df302009-06-17 22:02:04 +00001580 return state;
Zhongxing Xub393b502008-10-31 10:53:01 +00001581
Ted Kremenekfa417142009-08-06 01:20:57 +00001582 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001583 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremenekfa417142009-08-06 01:20:57 +00001584 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001585
Ted Kremenek609df302009-06-17 22:02:04 +00001586 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1587 // Ignore them and kill the field values.
1588 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001589 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001590
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001591 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001592 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001593
1594 RecordDecl::field_iterator FI, FE;
1595
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001596 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001597
Zhongxing Xu0442e962009-06-23 05:43:16 +00001598 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001599 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001600
Zhongxing Xub393b502008-10-31 10:53:01 +00001601 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001602 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001603
Ted Kremenek30030012009-09-22 21:19:14 +00001604 if (FTy->isArrayType())
Ted Kremenek609df302009-06-17 22:02:04 +00001605 state = BindArray(state, FR, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001606 else if (FTy->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001607 state = BindStruct(state, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001608 else
1609 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001610 }
1611
Zhongxing Xu0442e962009-06-23 05:43:16 +00001612 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001613 if (FI != FE) {
1614 Store store = state->getStore();
1615 RegionBindings B = GetRegionBindings(store);
1616 B = RBFactory.Add(B, R,
1617 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1618 state = state->makeWithStore(B.getRoot());
1619 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001620
Ted Kremenek609df302009-06-17 22:02:04 +00001621 return state;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001622}
1623
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001624Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1625 RegionBindings B = GetRegionBindings(store);
1626 llvm::OwningPtr<RegionStoreSubRegionMap>
1627 SubRegions(getRegionStoreSubRegionMap(store));
1628 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001629
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001630 // Set the default value of the struct region to "unknown".
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001631 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xucff637a2009-01-13 01:49:57 +00001632
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001633 return B.getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001634}
1635
Ted Kremenekfa417142009-08-06 01:20:57 +00001636const GRState*
1637RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1638 const GRState *state,
1639 const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001640
Ted Kremenekfa417142009-08-06 01:20:57 +00001641 // Nuke the old bindings stemming from R.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001642 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekfa417142009-08-06 01:20:57 +00001643
Mike Stump11289f42009-09-09 15:08:12 +00001644 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001645 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenekfa417142009-08-06 01:20:57 +00001646
Mike Stump11289f42009-09-09 15:08:12 +00001647 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001648 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001649
Ted Kremenekfa417142009-08-06 01:20:57 +00001650 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1651 // results in a zero-copy algorithm.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001652 return state->makeWithStore(
1653 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenekfa417142009-08-06 01:20:57 +00001654}
Mike Stump11289f42009-09-09 15:08:12 +00001655
Ted Kremenek4533a552009-06-16 22:36:44 +00001656//===----------------------------------------------------------------------===//
1657// State pruning.
1658//===----------------------------------------------------------------------===//
Ted Kremenekcc224242009-09-29 06:35:00 +00001659
Mike Stump11289f42009-09-09 15:08:12 +00001660void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenekcee28a42009-08-02 04:45:08 +00001661 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001662 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001663{
Ted Kremenek9f3a6432009-10-17 17:45:11 +00001664 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1665
Ted Kremenekcee28a42009-08-02 04:45:08 +00001666 Store store = state.getStore();
Ted Kremenek2c85f172009-08-06 04:50:20 +00001667 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001668
Ted Kremenek4533a552009-06-16 22:36:44 +00001669 // The backmap from regions to subregions.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001670 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001671 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekcc224242009-09-29 06:35:00 +00001672
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001673 // Do a pass over the regions in the store. For VarRegions we check if
1674 // the variable is still live and if so add it to the list of live roots.
1675 // For other regions we populate our region backmap.
Ted Kremenek4533a552009-06-16 22:36:44 +00001676 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenekcc224242009-09-29 06:35:00 +00001677
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001678 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001679 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001680 const MemRegion *R = I.getKey();
1681 IntermediateRoots.push_back(R);
Ted Kremenek4533a552009-06-16 22:36:44 +00001682 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001683
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001684 // Process the "intermediate" roots to find if they are referenced by
Mike Stump11289f42009-09-09 15:08:12 +00001685 // real roots.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001686 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001687 llvm::SmallVector<RBDNode, 10> Postponed;
1688
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001689 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001690
Ted Kremenek4533a552009-06-16 22:36:44 +00001691 while (!IntermediateRoots.empty()) {
1692 const MemRegion* R = IntermediateRoots.back();
1693 IntermediateRoots.pop_back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001694
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001695 if (IntermediateVisited.count(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001696 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001697 IntermediateVisited.insert(R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001698
Ted Kremenek4533a552009-06-16 22:36:44 +00001699 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekc32f2c22009-12-04 20:32:20 +00001700 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001701 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001702 continue;
1703 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001704
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001705 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001706 llvm::SmallVectorImpl<RBDNode> &Q =
1707 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1708
1709 Q.push_back(std::make_pair(&state, SR));
1710
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001711 continue;
Ted Kremenek4533a552009-06-16 22:36:44 +00001712 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001713
1714 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001715 if (const SubRegion* superR =
Ted Kremenekcc224242009-09-29 06:35:00 +00001716 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001717 IntermediateRoots.push_back(superR);
Ted Kremenek4533a552009-06-16 22:36:44 +00001718 }
Mike Stump11289f42009-09-09 15:08:12 +00001719
Ted Kremenekcc224242009-09-29 06:35:00 +00001720 // Enqueue the RegionRoots onto WorkList.
1721 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1722 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001723 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump11289f42009-09-09 15:08:12 +00001724 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001725 RegionRoots.clear();
1726
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001727 llvm::DenseSet<RBDNode> Visited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001728
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001729tryAgain:
Ted Kremenekcc224242009-09-29 06:35:00 +00001730 while (!WorkList.empty()) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001731 RBDNode N = WorkList.back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001732 WorkList.pop_back();
1733
1734 // Have we visited this node before?
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001735 if (Visited.count(N))
Ted Kremenekcc224242009-09-29 06:35:00 +00001736 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001737 Visited.insert(N);
Mike Stump11289f42009-09-09 15:08:12 +00001738
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001739 const MemRegion *R = N.second;
1740 const GRState *state_N = N.first;
Ted Kremenekcc224242009-09-29 06:35:00 +00001741
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001742 // Enqueue subregions.
1743 RegionStoreSubRegionMap *M;
1744
1745 if (&state == state_N)
1746 M = SubRegions.get();
1747 else {
1748 RegionStoreSubRegionMap *& SM = SC[state_N];
1749 if (!SM)
1750 SM = getRegionStoreSubRegionMap(state_N->getStore());
1751 M = SM;
1752 }
1753
1754 RegionStoreSubRegionMap::iterator I, E;
1755 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1756 WorkList.push_back(std::make_pair(state_N, *I));
1757
Ted Kremenekcc224242009-09-29 06:35:00 +00001758 // Enqueue the super region.
1759 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1760 const MemRegion *superR = SR->getSuperRegion();
1761 if (!isa<MemSpaceRegion>(superR)) {
1762 // If 'R' is a field or an element, we want to keep the bindings
1763 // for the other fields and elements around. The reason is that
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001764 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8b2f5d32009-10-17 07:32:08 +00001765 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1766 || isa<ObjCIvarRegion>(R));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001767 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenekcc224242009-09-29 06:35:00 +00001768 }
1769 }
1770
1771 // Mark the symbol for any live SymbolicRegion as "live". This means we
1772 // should continue to track that symbol.
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001773 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001774 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001775
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001776 // For BlockDataRegions, enqueue the VarRegions for variables marked
1777 // with __block (passed-by-reference).
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001778 // via BlockDeclRefExprs.
1779 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1780 for (BlockDataRegion::referenced_vars_iterator
1781 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremenek4b349cc2009-12-03 17:48:05 +00001782 RI != RE; ++RI) {
1783 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1784 WorkList.push_back(std::make_pair(state_N, *RI));
1785 }
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001786 // No possible data bindings on a BlockDataRegion. Continue to the
1787 // next region in the worklist.
1788 continue;
1789 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001790
1791 Store store_N = state_N->getStore();
1792 RegionBindings B_N = GetRegionBindings(store_N);
1793
1794 // Get the data binding for R (if any).
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001795 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001796
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001797 if (V) {
1798 // Check for lazy bindings.
1799 if (const nonloc::LazyCompoundVal *LCV =
1800 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001801
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001802 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001803 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001804 }
1805 else {
Ted Kremenekcc224242009-09-29 06:35:00 +00001806 // Update the set of live symbols.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001807 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenekcc224242009-09-29 06:35:00 +00001808 SI!=SE;++SI)
1809 SymReaper.markLive(*SI);
1810
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001811 // If V is a region, then add it to the worklist.
1812 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001813 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenekcc224242009-09-29 06:35:00 +00001814 }
1815 }
1816 }
1817
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001818 // See if any postponed SymbolicRegions are actually live now, after
1819 // having done a scan.
1820 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1821 E = Postponed.end() ; I != E ; ++I) {
1822 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1823 if (SymReaper.isLive(SR->getSymbol())) {
1824 WorkList.push_back(*I);
1825 I->second = NULL;
1826 }
1827 }
1828 }
1829
1830 if (!WorkList.empty())
1831 goto tryAgain;
1832
Ted Kremenek4533a552009-06-16 22:36:44 +00001833 // We have now scanned the store, marking reachable regions and symbols
1834 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00001835 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001836 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001837 const MemRegion* R = I.getKey();
Ted Kremenek4533a552009-06-16 22:36:44 +00001838 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001839 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek4533a552009-06-16 22:36:44 +00001840 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001841
Ted Kremenek4533a552009-06-16 22:36:44 +00001842 // Remove this dead region from the store.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001843 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump11289f42009-09-09 15:08:12 +00001844
Ted Kremenek4533a552009-06-16 22:36:44 +00001845 // Mark all non-live symbols that this region references as dead.
1846 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1847 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00001848
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001849 SVal X = *I.getData().getValue();
Ted Kremenekf106ab92009-08-02 05:00:15 +00001850 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1851 for (; SI != SE; ++SI)
1852 SymReaper.maybeDead(*SI);
1853 }
Mike Stump11289f42009-09-09 15:08:12 +00001854
Ted Kremenekcee28a42009-08-02 04:45:08 +00001855 // Write the store back.
1856 state.setStore(store);
Ted Kremenek4533a552009-06-16 22:36:44 +00001857}
1858
Zhongxing Xudaa41762009-10-13 02:24:55 +00001859GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1860 StackFrameContext const *frame) {
1861 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1862 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1863
1864 FunctionDecl::param_const_iterator PI = FD->param_begin();
1865
1866 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1867
1868 // Copy the arg expression value to the arg variables.
1869 for (; AI != AE; ++AI, ++PI) {
1870 SVal ArgVal = state->getSVal(*AI);
Ted Kremenek721fcc02009-12-04 00:26:31 +00001871 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xudaa41762009-10-13 02:24:55 +00001872 }
1873
1874 return state;
1875}
1876
Ted Kremenek4533a552009-06-16 22:36:44 +00001877//===----------------------------------------------------------------------===//
1878// Utility methods.
1879//===----------------------------------------------------------------------===//
1880
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001881void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00001882 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00001883 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00001884 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00001885
Ted Kremenek2c85f172009-08-06 04:50:20 +00001886 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001887 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00001888}