blob: 170abc8fe6f1ffd202d190d0b9586fb302730344 [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 }
525
526 // Handle the region itself.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000527 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
528 isa<ObjCObjectRegion>(R)) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000529 // Invalidate the region by setting its default value to
530 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000531 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
532 Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000533 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000534 continue;
535 }
Mike Stump11289f42009-09-09 15:08:12 +0000536
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000537 if (!R->isBoundable())
538 continue;
539
540 const TypedRegion *TR = cast<TypedRegion>(R);
541 QualType T = TR->getValueType(Ctx);
542
543 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000544 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
545
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000546 // No record definition. There is nothing we can do.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000547 if (!RD)
548 continue;
549
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000550 // Invalidate the region by setting its default value to
551 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000552 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
553 Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000554 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000555 continue;
556 }
557
558 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
559 // Set the default value of the array to conjured symbol.
560 DefinedOrUnknownSVal V =
561 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000562 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000563 continue;
564 }
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000565
566 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
567 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000568 // For fields and elements whose super region has also been invalidated,
569 // only remove the old binding. The super region will get set with a
570 // default value from which we can lazily derive a new symbolic value.
Ted Kremenek5daec8a2009-09-29 03:34:03 +0000571 B = RBFactory.Remove(B, R);
572 continue;
573 }
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000574
Ted Kremenek1cbdf6e2009-09-29 03:12:50 +0000575 // Invalidate the binding.
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000576 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
577 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000578 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremenekfa417142009-08-06 01:20:57 +0000579 }
580
Ted Kremeneke41b81e2009-09-27 20:45:21 +0000581 // Create a new state with the updated bindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000582 return state->makeWithStore(B.getRoot());
Ted Kremenekbca70672009-07-29 18:16:25 +0000583}
584
585//===----------------------------------------------------------------------===//
Ted Kremenek4533a552009-06-16 22:36:44 +0000586// getLValueXXX methods.
587//===----------------------------------------------------------------------===//
588
Ted Kremenek2907ab72008-12-24 07:46:32 +0000589/// getLValueString - Returns an SVal representing the lvalue of a
590/// StringLiteral. Within RegionStore a StringLiteral has an
591/// associated StringRegion, and the lvalue of a StringLiteral is the
592/// lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000593SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu0d2706f2008-10-25 14:18:57 +0000594 return loc::MemRegionVal(MRMgr.getStringRegion(S));
595}
596
Ted Kremenek2907ab72008-12-24 07:46:32 +0000597/// getLValueVar - Returns an SVal that represents the lvalue of a
598/// variable. Within RegionStore a variable has an associated
599/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000600SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenek14536f62009-08-21 22:28:32 +0000601 const LocationContext *LC) {
602 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000603}
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000604
Ted Kremenek2907ab72008-12-24 07:46:32 +0000605/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
606/// of a compound literal. Within RegionStore a compound literal
607/// has an associated region, and the lvalue of the compound literal
608/// is the lvalue of that region.
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000609SVal
610RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000611 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
612}
613
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000614SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
615 return getLValueFieldOrIvar(D, Base);
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000616}
617
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000618SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
619 return getLValueFieldOrIvar(D, Base);
Ted Kremenekd3c82762009-03-05 04:50:08 +0000620}
621
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000622SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000623 if (Base.isUnknownOrUndef())
624 return Base;
625
626 Loc BaseL = cast<Loc>(Base);
627 const MemRegion* BaseR = 0;
628
629 switch (BaseL.getSubKind()) {
630 case loc::MemRegionKind:
631 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
632 break;
633
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000634 case loc::GotoLabelKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000635 // These are anormal cases. Flag an undefined value.
636 return UndefinedVal();
637
638 case loc::ConcreteIntKind:
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000639 // While these seem funny, this can happen through casts.
640 // FIXME: What we should return is the field offset. For example,
641 // add the field offset to the integer value. That way funny things
642 // like this work properly: &(((struct foo *) 0xa)->f)
643 return Base;
644
645 default:
Zhongxing Xue79a4e62008-11-07 08:57:30 +0000646 assert(0 && "Unhandled Base.");
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000647 return Base;
648 }
Mike Stump11289f42009-09-09 15:08:12 +0000649
Ted Kremenekd3c82762009-03-05 04:50:08 +0000650 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
651 // of FieldDecl.
652 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
653 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000654
Ted Kremenekd3c82762009-03-05 04:50:08 +0000655 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xu2fbc3542008-10-22 13:44:38 +0000656}
657
Zhongxing Xu7d6387b2009-10-14 03:33:08 +0000658SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
659 SVal Base) {
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000660
Ted Kremenek06032222009-03-09 22:44:49 +0000661 // If the base is an unknown or undefined value, just return it back.
662 // FIXME: For absolute pointer addresses, we just return that value back as
663 // well, although in reality we should return the offset added to that
664 // value.
665 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu36d4ade2008-10-27 12:23:17 +0000666 return Base;
667
Ted Kremenek92d48a72009-01-22 20:27:48 +0000668 // Only handle integer offsets... for now.
669 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000670 return UnknownVal();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000671
Zhongxing Xu7c382642009-05-09 13:20:07 +0000672 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremenek92d48a72009-01-22 20:27:48 +0000673
674 // Pointer of any type can be cast and used as array base.
675 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000677 // Convert the offset to the appropriate size and signedness.
678 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenek92d48a72009-01-22 20:27:48 +0000680 if (!ElemR) {
681 //
682 // If the base region is not an ElementRegion, create one.
683 // This can happen in the following example:
684 //
685 // char *p = __builtin_alloc(10);
686 // p[1] = 8;
687 //
Zhongxing Xu7c382642009-05-09 13:20:07 +0000688 // Observe that 'p' binds to an AllocaRegion.
Ted Kremenek92d48a72009-01-22 20:27:48 +0000689 //
Ted Kremenek02e50892009-05-04 06:18:28 +0000690 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000691 BaseRegion, getContext()));
Zhongxing Xud4e72fc2008-11-13 09:48:44 +0000692 }
Mike Stump11289f42009-09-09 15:08:12 +0000693
Ted Kremenek92d48a72009-01-22 20:27:48 +0000694 SVal BaseIdx = ElemR->getIndex();
Mike Stump11289f42009-09-09 15:08:12 +0000695
Ted Kremenek92d48a72009-01-22 20:27:48 +0000696 if (!isa<nonloc::ConcreteInt>(BaseIdx))
697 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenek92d48a72009-01-22 20:27:48 +0000699 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
700 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
701 assert(BaseIdxI.isSigned());
Mike Stump11289f42009-09-09 15:08:12 +0000702
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000703 // Compute the new index.
704 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump11289f42009-09-09 15:08:12 +0000705
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000706 // Construct the new ElementRegion.
707 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu838a0db2009-06-16 09:55:50 +0000708 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump11289f42009-09-09 15:08:12 +0000709 getContext()));
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000710}
711
Ted Kremenek4533a552009-06-16 22:36:44 +0000712//===----------------------------------------------------------------------===//
713// Extents for regions.
714//===----------------------------------------------------------------------===//
715
Zhongxing Xu383c2732009-11-12 02:48:32 +0000716DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
717 const MemRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +0000718
Ted Kremenek94575aa2009-07-10 22:30:06 +0000719 switch (R->getKind()) {
720 case MemRegion::MemSpaceRegionKind:
721 assert(0 && "Cannot index into a MemSpace");
Mike Stump11289f42009-09-09 15:08:12 +0000722 return UnknownVal();
723
Ted Kremenek10a50e72009-11-25 01:32:22 +0000724 case MemRegion::FunctionTextRegionKind:
725 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000726 case MemRegion::BlockDataRegionKind:
Ted Kremenek94575aa2009-07-10 22:30:06 +0000727 // Technically this can happen if people do funny things with casts.
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000728 return UnknownVal();
Ted Kremenek94575aa2009-07-10 22:30:06 +0000729
730 // Not yet handled.
731 case MemRegion::AllocaRegionKind:
732 case MemRegion::CompoundLiteralRegionKind:
733 case MemRegion::ElementRegionKind:
734 case MemRegion::FieldRegionKind:
735 case MemRegion::ObjCIvarRegionKind:
736 case MemRegion::ObjCObjectRegionKind:
737 case MemRegion::SymbolicRegionKind:
738 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000739
Ted Kremenek94575aa2009-07-10 22:30:06 +0000740 case MemRegion::StringRegionKind: {
741 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump11289f42009-09-09 15:08:12 +0000742 // We intentionally made the size value signed because it participates in
Ted Kremenek94575aa2009-07-10 22:30:06 +0000743 // operations with signed indices.
744 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek7594e2a2009-01-30 00:08:43 +0000745 }
Mike Stump11289f42009-09-09 15:08:12 +0000746
Ted Kremenek94575aa2009-07-10 22:30:06 +0000747 case MemRegion::VarRegionKind: {
748 const VarRegion* VR = cast<VarRegion>(R);
749 // Get the type of the variable.
750 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000751
Ted Kremenek94575aa2009-07-10 22:30:06 +0000752 // FIXME: Handle variable-length arrays.
753 if (isa<VariableArrayType>(T))
754 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000755
Ted Kremenek94575aa2009-07-10 22:30:06 +0000756 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
757 // return the size as signed integer.
758 return ValMgr.makeIntVal(CAT->getSize(), false);
759 }
Ted Kremenekca7935d2009-08-02 05:15:23 +0000760
Ted Kremenek94575aa2009-07-10 22:30:06 +0000761 // Clients can use ordinary variables as if they were arrays. These
762 // essentially are arrays of size 1.
763 return ValMgr.makeIntVal(1, false);
Zhongxing Xuea8c48d2009-05-06 11:51:48 +0000764 }
Mike Stump11289f42009-09-09 15:08:12 +0000765
Ted Kremenek94575aa2009-07-10 22:30:06 +0000766 case MemRegion::BEG_DECL_REGIONS:
767 case MemRegion::END_DECL_REGIONS:
768 case MemRegion::BEG_TYPED_REGIONS:
769 case MemRegion::END_TYPED_REGIONS:
770 assert(0 && "Infeasible region");
771 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Ted Kremenek94575aa2009-07-10 22:30:06 +0000774 assert(0 && "Unreachable");
Ted Kremenek47ad37d2009-01-06 19:12:06 +0000775 return UnknownVal();
Zhongxing Xu4d45b342008-11-22 13:21:46 +0000776}
777
Ted Kremenek609df302009-06-17 22:02:04 +0000778const GRState *RegionStoreManager::setExtent(const GRState *state,
779 const MemRegion *region,
780 SVal extent) {
781 return state->set<RegionExtents>(region, extent);
Ted Kremenek4533a552009-06-16 22:36:44 +0000782}
783
784//===----------------------------------------------------------------------===//
785// Location and region casting.
786//===----------------------------------------------------------------------===//
787
Ted Kremenek2907ab72008-12-24 07:46:32 +0000788/// ArrayToPointer - Emulates the "decay" of an array to a pointer
789/// type. 'Array' represents the lvalue of the array being decayed
790/// to a pointer, and the returned SVal represents the decayed
791/// version of that lvalue (i.e., a pointer to the first element of
792/// the array). This is called by GRExprEngine when evaluating casts
793/// from arrays to pointers.
Zhongxing Xua865b792009-03-30 05:55:46 +0000794SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekf065b152008-12-13 19:24:37 +0000795 if (!isa<loc::MemRegionVal>(Array))
796 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000797
Ted Kremenekf065b152008-12-13 19:24:37 +0000798 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
799 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump11289f42009-09-09 15:08:12 +0000800
Ted Kremenek167f2fa2009-01-13 01:03:27 +0000801 if (!ArrayR)
Ted Kremenekf065b152008-12-13 19:24:37 +0000802 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000803
Zhongxing Xu34d04b32009-05-09 03:57:34 +0000804 // Strip off typedefs from the ArrayRegion's ValueType.
John McCalla1925362009-09-29 23:03:30 +0000805 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenek02e50892009-05-04 06:18:28 +0000806 ArrayType *AT = cast<ArrayType>(T);
807 T = AT->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000808
Ted Kremenekccc22922009-07-16 00:00:11 +0000809 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
810 ElementRegion* ER = MRMgr.getElementRegion(T, ZeroIdx, ArrayR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000811
812 return loc::MemRegionVal(ER);
Zhongxing Xua8d2cbe2008-10-24 01:09:32 +0000813}
814
Ted Kremenek4533a552009-06-16 22:36:44 +0000815//===----------------------------------------------------------------------===//
816// Pointer arithmetic.
817//===----------------------------------------------------------------------===//
818
Mike Stump11289f42009-09-09 15:08:12 +0000819SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenekaf1ac822009-06-26 00:41:43 +0000820 BinaryOperator::Opcode Op, Loc L, NonLoc R,
821 QualType resultTy) {
Zhongxing Xud6daef92009-05-09 15:18:12 +0000822 // Assume the base location is MemRegionVal.
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000823 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xue7d14932009-03-02 07:52:23 +0000824 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000825
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000826 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xud6daef92009-05-09 15:18:12 +0000827 const ElementRegion *ER = 0;
Zhongxing Xua7907602009-05-20 09:00:16 +0000828
Ted Kremenekf6f04612009-07-11 00:58:27 +0000829 switch (MR->getKind()) {
830 case MemRegion::SymbolicRegionKind: {
831 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000832 SymbolRef Sym = SR->getSymbol();
Ted Kremenekd1d60662009-08-25 22:55:09 +0000833 QualType T = Sym->getType(getContext());
834 QualType EleTy;
Mike Stump11289f42009-09-09 15:08:12 +0000835
Ted Kremenekd1d60662009-08-25 22:55:09 +0000836 if (const PointerType *PT = T->getAs<PointerType>())
837 EleTy = PT->getPointeeType();
838 else
John McCall9dd450b2009-09-21 23:43:11 +0000839 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump11289f42009-09-09 15:08:12 +0000840
Ted Kremenekf6f04612009-07-11 00:58:27 +0000841 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
842 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000843 break;
Zhongxing Xucc457622009-06-19 04:51:14 +0000844 }
Ted Kremenekf6f04612009-07-11 00:58:27 +0000845 case MemRegion::AllocaRegionKind: {
Ted Kremenekf6f04612009-07-11 00:58:27 +0000846 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekca7935d2009-08-02 05:15:23 +0000847 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000848 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenekf6f04612009-07-11 00:58:27 +0000849 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
850 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump11289f42009-09-09 15:08:12 +0000851 break;
Ted Kremenekf6f04612009-07-11 00:58:27 +0000852 }
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000853
Ted Kremenekf6f04612009-07-11 00:58:27 +0000854 case MemRegion::ElementRegionKind: {
855 ER = cast<ElementRegion>(MR);
856 break;
857 }
Mike Stump11289f42009-09-09 15:08:12 +0000858
Ted Kremenekf6f04612009-07-11 00:58:27 +0000859 // Not yet handled.
860 case MemRegion::VarRegionKind:
Ted Kremenek8ec57712009-10-06 01:39:48 +0000861 case MemRegion::StringRegionKind: {
862
863 }
864 // Fall-through.
Ted Kremenekf6f04612009-07-11 00:58:27 +0000865 case MemRegion::CompoundLiteralRegionKind:
866 case MemRegion::FieldRegionKind:
867 case MemRegion::ObjCObjectRegionKind:
868 case MemRegion::ObjCIvarRegionKind:
869 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000870
Ted Kremenek10a50e72009-11-25 01:32:22 +0000871 case MemRegion::FunctionTextRegionKind:
872 case MemRegion::BlockTextRegionKind:
Ted Kremenekb63ad7a2009-11-25 23:53:07 +0000873 case MemRegion::BlockDataRegionKind:
Ted Kremenekf6f04612009-07-11 00:58:27 +0000874 // Technically this can happen if people do funny things with casts.
875 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000876
Ted Kremenekf6f04612009-07-11 00:58:27 +0000877 case MemRegion::MemSpaceRegionKind:
878 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
879 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +0000880
Ted Kremenekf6f04612009-07-11 00:58:27 +0000881 case MemRegion::BEG_DECL_REGIONS:
882 case MemRegion::END_DECL_REGIONS:
883 case MemRegion::BEG_TYPED_REGIONS:
884 case MemRegion::END_TYPED_REGIONS:
885 assert(0 && "Infeasible region");
886 return UnknownVal();
Zhongxing Xu540c0092009-06-21 13:24:24 +0000887 }
Zhongxing Xu507202e2009-03-11 07:43:49 +0000888
Zhongxing Xue7d14932009-03-02 07:52:23 +0000889 SVal Idx = ER->getIndex();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000890 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xue7d14932009-03-02 07:52:23 +0000891
Ted Kremenek8ec57712009-10-06 01:39:48 +0000892 // For now, only support:
893 // (a) concrete integer indices that can easily be resolved
894 // (b) 0 + symbolic index
895 if (Base) {
896 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
897 // FIXME: Should use SValuator here.
898 SVal NewIdx =
899 Base->evalBinOp(ValMgr, Op,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +0000900 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenek8ec57712009-10-06 01:39:48 +0000901 const MemRegion* NewER =
902 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
903 ER->getSuperRegion(), getContext());
904 return ValMgr.makeLoc(NewER);
905 }
906 if (0 == Base->getValue()) {
907 const MemRegion* NewER =
908 MRMgr.getElementRegion(ER->getElementType(), R,
909 ER->getSuperRegion(), getContext());
910 return ValMgr.makeLoc(NewER);
911 }
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000912 }
Mike Stump11289f42009-09-09 15:08:12 +0000913
Ted Kremenek4c8a5812009-03-03 02:51:43 +0000914 return UnknownVal();
Zhongxing Xue7d14932009-03-02 07:52:23 +0000915}
916
Ted Kremenek4533a552009-06-16 22:36:44 +0000917//===----------------------------------------------------------------------===//
918// Loading values from regions.
919//===----------------------------------------------------------------------===//
920
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000921Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
922 const MemRegion *R) {
923 if (const BindingVal *BV = B.lookup(R))
924 return Optional<SVal>::create(BV->getDirectValue());
925
926 return Optional<SVal>();
927}
928
929Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000930 const MemRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +0000931
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000932 if (R->isBoundable())
933 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
934 if (TR->getValueType(getContext())->isUnionType())
935 return UnknownVal();
936
Zhongxing Xub8edf2a2009-10-11 08:08:02 +0000937 if (BindingVal const *V = B.lookup(R))
938 return Optional<SVal>::create(V->getDefaultValue());
939
940 return Optional<SVal>();
941}
942
943Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
944 const MemRegion *R) {
945 if (const BindingVal *BV = B.lookup(R))
946 return Optional<SVal>::create(BV->getValue());
947
948 return Optional<SVal>();
Ted Kremenek2f6eb142009-08-06 21:43:54 +0000949}
950
Ted Kremeneke6fea682009-07-15 02:31:43 +0000951static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
952 RTy = Ctx.getCanonicalType(RTy);
953 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump11289f42009-09-09 15:08:12 +0000954
Ted Kremeneke6fea682009-07-15 02:31:43 +0000955 if (RTy == UsedTy)
956 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000957
958
Ted Kremenek834e2f62009-07-20 22:58:02 +0000959 // Recursively check the types. We basically want to see if a pointer value
Mike Stump11289f42009-09-09 15:08:12 +0000960 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek834e2f62009-07-20 22:58:02 +0000961 // represents a reinterpretation.
962 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump11289f42009-09-09 15:08:12 +0000963 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000964 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek834e2f62009-07-20 22:58:02 +0000965
966 return PUsedTy && PRTy &&
967 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump11289f42009-09-09 15:08:12 +0000968 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek834e2f62009-07-20 22:58:02 +0000969 }
970
971 return true;
Ted Kremeneke6fea682009-07-15 02:31:43 +0000972}
973
Ted Kremenek267e45a2009-09-24 04:11:44 +0000974const ElementRegion *
975RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
976 ASTContext &Ctx = getContext();
977 SVal idx = ValMgr.makeZeroArrayIndex();
978 assert(!T.isNull());
979 return MRMgr.getElementRegion(T, idx, SR, Ctx);
980}
981
982
983
Ted Kremenekac7c7242009-07-21 21:03:30 +0000984SValuator::CastResult
985RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek609df302009-06-17 22:02:04 +0000986
Zhongxing Xu83aff702008-10-21 05:29:26 +0000987 assert(!isa<UnknownVal>(L) && "location unknown");
988 assert(!isa<UndefinedVal>(L) && "location undefined");
989
Ted Kremenek2907ab72008-12-24 07:46:32 +0000990 // FIXME: Is this even possible? Shouldn't this be treated as a null
991 // dereference at a higher level?
Zhongxing Xuaf7415f2008-12-20 06:32:12 +0000992 if (isa<loc::ConcreteInt>(L))
Ted Kremenekac7c7242009-07-21 21:03:30 +0000993 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu83aff702008-10-21 05:29:26 +0000994
Ted Kremenek609df302009-06-17 22:02:04 +0000995 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000996
Zhongxing Xu1075cc02009-05-20 09:18:48 +0000997 // FIXME: return symbolic value for these cases.
Zhongxing Xuec7e7df2009-04-03 07:33:13 +0000998 // Example:
999 // void f(int* p) { int x = *p; }
Zhongxing Xu1075cc02009-05-20 09:18:48 +00001000 // char* p = alloca();
1001 // read(p);
1002 // c = *p;
Ted Kremenek0c37d192009-07-14 20:48:22 +00001003 if (isa<AllocaRegion>(MR))
Ted Kremenekac7c7242009-07-21 21:03:30 +00001004 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001005
Ted Kremenek267e45a2009-09-24 04:11:44 +00001006 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1007 MR = GetElementZeroRegion(SR, T);
Mike Stump11289f42009-09-09 15:08:12 +00001008
Ted Kremenek0bb32e32009-08-03 21:41:46 +00001009 if (isa<CodeTextRegion>(MR))
1010 return SValuator::CastResult(state, UnknownVal());
Mike Stump11289f42009-09-09 15:08:12 +00001011
Ted Kremenek2907ab72008-12-24 07:46:32 +00001012 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1013 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek609df302009-06-17 22:02:04 +00001014 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001015 QualType RTy = R->getValueType(getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001016
Ted Kremenek2907ab72008-12-24 07:46:32 +00001017 // FIXME: We should eventually handle funny addressing. e.g.:
1018 //
1019 // int x = ...;
1020 // int *p = &x;
1021 // char *q = (char*) p;
1022 // char c = *q; // returns the first byte of 'x'.
1023 //
1024 // Such funny addressing will occur due to layering of regions.
1025
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001026#if 0
Ted Kremeneke6fea682009-07-15 02:31:43 +00001027 ASTContext &Ctx = getContext();
1028 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001029 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1030 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneke6fea682009-07-15 02:31:43 +00001031 RTy = T;
Ted Kremenek57fa7e32009-07-15 04:23:32 +00001032 assert(Ctx.getCanonicalType(RTy) ==
1033 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump11289f42009-09-09 15:08:12 +00001034 }
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001035#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001036
Zhongxing Xuce270a62009-03-09 09:15:51 +00001037 if (RTy->isStructureType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001038 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump11289f42009-09-09 15:08:12 +00001039
Ted Kremenek2f6eb142009-08-06 21:43:54 +00001040 // FIXME: Handle unions.
1041 if (RTy->isUnionType())
1042 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001043
1044 if (RTy->isArrayType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001045 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001046
Zhongxing Xuce270a62009-03-09 09:15:51 +00001047 // FIXME: handle Vector types.
1048 if (RTy->isVectorType())
Ted Kremenekac7c7242009-07-21 21:03:30 +00001049 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu0628f532009-06-28 14:16:39 +00001050
1051 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001052 return SValuator::CastResult(state,
1053 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu0628f532009-06-28 14:16:39 +00001054
1055 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001056 return SValuator::CastResult(state,
1057 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump11289f42009-09-09 15:08:12 +00001058
Ted Kremenek834e2f62009-07-20 22:58:02 +00001059 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001060 return SValuator::CastResult(state,
1061 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump11289f42009-09-09 15:08:12 +00001062
Ted Kremenekfe12f882009-07-21 00:12:07 +00001063 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu731f4622009-11-16 04:49:44 +00001064 return SValuator::CastResult(state,
1065 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek834e2f62009-07-20 22:58:02 +00001066
Ted Kremenek2c85f172009-08-06 04:50:20 +00001067 RegionBindings B = GetRegionBindings(state->getStore());
1068 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001069
1070 // Check if the region has a binding.
1071 if (V)
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001072 if (SVal const *SV = V->getValue())
1073 return SValuator::CastResult(state, *SV);
Ted Kremenek2907ab72008-12-24 07:46:32 +00001074
Ted Kremenek2907ab72008-12-24 07:46:32 +00001075 // The location does not have a bound value. This means that it has
1076 // the value it had upon its creation and/or entry to the analyzed
1077 // function/method. These are either symbolic values or 'undefined'.
1078
Ted Kremenek920ad712009-07-22 04:35:42 +00001079#if HEAP_UNDEFINED
Ted Kremenek2d99f972009-06-23 18:17:08 +00001080 if (R->hasHeapOrStackStorage()) {
Ted Kremenek920ad712009-07-22 04:35:42 +00001081#else
1082 if (R->hasStackStorage()) {
1083#endif
Ted Kremenek2907ab72008-12-24 07:46:32 +00001084 // All stack variables are considered to have undefined values
1085 // upon creation. All heap allocated blocks are considered to
1086 // have undefined values as well unless they are explicitly bound
1087 // to specific values.
Ted Kremenekac7c7242009-07-21 21:03:30 +00001088 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek2907ab72008-12-24 07:46:32 +00001089 }
1090
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001091 // All other values are symbolic.
Ted Kremenekac7c7242009-07-21 21:03:30 +00001092 return SValuator::CastResult(state,
1093 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu83aff702008-10-21 05:29:26 +00001094}
Mike Stump11289f42009-09-09 15:08:12 +00001095
Ted Kremenekfa417142009-08-06 01:20:57 +00001096std::pair<const GRState*, const MemRegion*>
Ted Kremenek2c85f172009-08-06 04:50:20 +00001097RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001098 if (Optional<SVal> OV = getDirectBinding(B, R))
1099 if (const nonloc::LazyCompoundVal *V =
1100 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1101 return std::make_pair(V->getState(), V->getRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001102
Ted Kremenekfa417142009-08-06 01:20:57 +00001103 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1104 const std::pair<const GRState *, const MemRegion *> &X =
1105 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001106
Ted Kremenekfa417142009-08-06 01:20:57 +00001107 if (X.first)
1108 return std::make_pair(X.first,
1109 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump11289f42009-09-09 15:08:12 +00001110 }
Ted Kremenekfa417142009-08-06 01:20:57 +00001111 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1112 const std::pair<const GRState *, const MemRegion *> &X =
1113 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump11289f42009-09-09 15:08:12 +00001114
Ted Kremenekfa417142009-08-06 01:20:57 +00001115 if (X.first)
1116 return std::make_pair(X.first,
1117 MRMgr.getFieldRegionWithSuper(FR, X.second));
1118 }
1119
1120 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1121}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001122
Zhongxing Xu2d160732009-06-25 05:29:39 +00001123SVal RegionStoreManager::RetrieveElement(const GRState* state,
1124 const ElementRegion* R) {
1125 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001126 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001127 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu2d160732009-06-25 05:29:39 +00001128 return *V;
1129
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001130 const MemRegion* superR = R->getSuperRegion();
1131
Zhongxing Xu2d160732009-06-25 05:29:39 +00001132 // Check if the region is an element region of a string literal.
Ted Kremenek55e07ef2009-07-01 23:19:52 +00001133 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek228539f2009-09-29 16:36:48 +00001134 // FIXME: Handle loads from strings where the literal is treated as
1135 // an integer, e.g., *((unsigned int*)"hello")
1136 ASTContext &Ctx = getContext();
Douglas Gregor4ef1d402009-11-09 22:08:55 +00001137 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek228539f2009-09-29 16:36:48 +00001138 if (T != Ctx.getCanonicalType(R->getElementType()))
1139 return UnknownVal();
1140
Zhongxing Xu2d160732009-06-25 05:29:39 +00001141 const StringLiteral *Str = StrR->getStringLiteral();
1142 SVal Idx = R->getIndex();
1143 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1144 int64_t i = CI->getValue().getSExtValue();
Mike Stump11289f42009-09-09 15:08:12 +00001145 int64_t byteLength = Str->getByteLength();
Ted Kremenekb5850f92009-09-05 17:59:01 +00001146 if (i > byteLength) {
1147 // Buffer overflow checking in GRExprEngine should handle this case,
1148 // but we shouldn't rely on it to not overflow here if that checking
1149 // is disabled.
1150 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001151 }
Ted Kremenekb5850f92009-09-05 17:59:01 +00001152 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek228539f2009-09-29 16:36:48 +00001153 return ValMgr.makeIntVal(c, T);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001154 }
1155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Ted Kremenek040e3b92009-08-06 22:33:36 +00001157 // Check if the immediate super region has a direct binding.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001158 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneke6fea682009-07-15 02:31:43 +00001159 if (SymbolRef parentSym = V->getAsSymbol())
1160 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek920ad712009-07-22 04:35:42 +00001161
1162 if (V->isUnknownOrUndef())
1163 return *V;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001164
1165 // Handle LazyCompoundVals for the immediate super region. Other cases
1166 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump11289f42009-09-09 15:08:12 +00001167 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek040e3b92009-08-06 22:33:36 +00001168 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump11289f42009-09-09 15:08:12 +00001169
Ted Kremenek040e3b92009-08-06 22:33:36 +00001170 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1171 return RetrieveElement(LCV->getState(), R);
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Ted Kremeneke6fea682009-07-15 02:31:43 +00001174 // Other cases: give up.
Zhongxing Xu61e66922009-07-03 06:11:41 +00001175 return UnknownVal();
Zhongxing Xue205d43c2009-06-30 12:32:59 +00001176 }
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001177
Ted Kremenek040e3b92009-08-06 22:33:36 +00001178 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xu2d160732009-06-25 05:29:39 +00001179}
1180
Mike Stump11289f42009-09-09 15:08:12 +00001181SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001182 const FieldRegion* R) {
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001183
1184 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001185 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001186 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001187 return *V;
1188
Ted Kremenek040e3b92009-08-06 22:33:36 +00001189 QualType Ty = R->getValueType(getContext());
1190 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1191}
Mike Stump11289f42009-09-09 15:08:12 +00001192
Ted Kremenek040e3b92009-08-06 22:33:36 +00001193SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1194 const TypedRegion *R,
1195 QualType Ty,
1196 const MemRegion *superR) {
1197
Mike Stump11289f42009-09-09 15:08:12 +00001198 // At this point we have already checked in either RetrieveElement or
Ted Kremenek040e3b92009-08-06 22:33:36 +00001199 // RetrieveField if 'R' has a direct binding.
Mike Stump11289f42009-09-09 15:08:12 +00001200
Ted Kremenek040e3b92009-08-06 22:33:36 +00001201 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001203 while (superR) {
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001204 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001205 if (SymbolRef parentSym = D->getAsSymbol())
1206 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001208 if (D->isZeroConstant())
1209 return ValMgr.makeZeroVal(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001211 if (D->isUnknown())
1212 return *D;
Mike Stump11289f42009-09-09 15:08:12 +00001213
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001214 assert(0 && "Unknown default value");
1215 }
Mike Stump11289f42009-09-09 15:08:12 +00001216
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001217 // If our super region is a field or element itself, walk up the region
1218 // hierarchy to see if there is a default value installed in an ancestor.
1219 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1220 superR = cast<SubRegion>(superR)->getSuperRegion();
1221 continue;
1222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001224 break;
Ted Kremenekfa417142009-08-06 01:20:57 +00001225 }
Mike Stump11289f42009-09-09 15:08:12 +00001226
Ted Kremenekfa417142009-08-06 01:20:57 +00001227 // Lazy binding?
1228 const GRState *lazyBindingState = NULL;
Ted Kremenek040e3b92009-08-06 22:33:36 +00001229 const MemRegion *lazyBindingRegion = NULL;
1230 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump11289f42009-09-09 15:08:12 +00001231
Ted Kremenekfa417142009-08-06 01:20:57 +00001232 if (lazyBindingState) {
Ted Kremenek040e3b92009-08-06 22:33:36 +00001233 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump11289f42009-09-09 15:08:12 +00001234
Ted Kremenek040e3b92009-08-06 22:33:36 +00001235 if (isa<ElementRegion>(R))
1236 return RetrieveElement(lazyBindingState,
1237 cast<ElementRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001238
Ted Kremenekfa417142009-08-06 01:20:57 +00001239 return RetrieveField(lazyBindingState,
Ted Kremenek040e3b92009-08-06 22:33:36 +00001240 cast<FieldRegion>(lazyBindingRegion));
Mike Stump11289f42009-09-09 15:08:12 +00001241 }
1242
Ted Kremenek040e3b92009-08-06 22:33:36 +00001243 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump11289f42009-09-09 15:08:12 +00001244
Ted Kremenek040e3b92009-08-06 22:33:36 +00001245 if (isa<ElementRegion>(R)) {
1246 // Currently we don't reason specially about Clang-style vectors. Check
1247 // if superR is a vector and if so return Unknown.
1248 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1249 if (typedSuperR->getValueType(getContext())->isVectorType())
1250 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +00001251 }
Ted Kremenek040e3b92009-08-06 22:33:36 +00001252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001254 return UndefinedVal();
Ted Kremenek040e3b92009-08-06 22:33:36 +00001255 }
Mike Stump11289f42009-09-09 15:08:12 +00001256
Ted Kremenek06cc0e32009-07-02 22:16:42 +00001257 // All other values are symbolic.
1258 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xue67ea5c2009-06-25 04:50:44 +00001259}
Mike Stump11289f42009-09-09 15:08:12 +00001260
1261SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek48029552009-07-15 06:09:28 +00001262 const ObjCIvarRegion* R) {
1263
Ted Kremenek48029552009-07-15 06:09:28 +00001264 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001265 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek48029552009-07-15 06:09:28 +00001266
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001267 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek48029552009-07-15 06:09:28 +00001268 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001269
Ted Kremenek48029552009-07-15 06:09:28 +00001270 const MemRegion *superR = R->getSuperRegion();
1271
Ted Kremenek481c1212009-10-20 01:20:57 +00001272 // Check if the super region has a default binding.
1273 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek48029552009-07-15 06:09:28 +00001274 if (SymbolRef parentSym = V->getAsSymbol())
1275 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump11289f42009-09-09 15:08:12 +00001276
Ted Kremenek48029552009-07-15 06:09:28 +00001277 // Other cases: give up.
1278 return UnknownVal();
1279 }
Mike Stump11289f42009-09-09 15:08:12 +00001280
Ted Kremenek834e2f62009-07-20 22:58:02 +00001281 return RetrieveLazySymbol(state, R);
1282}
1283
Ted Kremenekfe12f882009-07-21 00:12:07 +00001284SVal RegionStoreManager::RetrieveVar(const GRState *state,
1285 const VarRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001286
Ted Kremenekfe12f882009-07-21 00:12:07 +00001287 // Check if the region has a binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001288 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump11289f42009-09-09 15:08:12 +00001289
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001290 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenekfe12f882009-07-21 00:12:07 +00001291 return *V;
Mike Stump11289f42009-09-09 15:08:12 +00001292
Ted Kremenekfe12f882009-07-21 00:12:07 +00001293 // Lazily derive a value for the VarRegion.
1294 const VarDecl *VD = R->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001295
Ted Kremenekfe12f882009-07-21 00:12:07 +00001296 if (R->hasGlobalsOrParametersStorage())
1297 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump11289f42009-09-09 15:08:12 +00001298
Ted Kremenekfe12f882009-07-21 00:12:07 +00001299 return UndefinedVal();
1300}
1301
Mike Stump11289f42009-09-09 15:08:12 +00001302SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek834e2f62009-07-20 22:58:02 +00001303 const TypedRegion *R) {
Mike Stump11289f42009-09-09 15:08:12 +00001304
Ted Kremenek834e2f62009-07-20 22:58:02 +00001305 QualType valTy = R->getValueType(getContext());
Ted Kremenek920ad712009-07-22 04:35:42 +00001306
Ted Kremenek48029552009-07-15 06:09:28 +00001307 // All other values are symbolic.
Ted Kremenek834e2f62009-07-20 22:58:02 +00001308 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek48029552009-07-15 06:09:28 +00001309}
1310
Mike Stump11289f42009-09-09 15:08:12 +00001311SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1312 const TypedRegion* R) {
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001313 QualType T = R->getValueType(getContext());
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001314 assert(T->isStructureType());
1315
Zhongxing Xud85a9912009-06-11 07:27:30 +00001316 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001317 RecordDecl* RD = RT->getDecl();
1318 assert(RD->isDefinition());
Mike Stumpc700b362009-08-06 12:56:50 +00001319 (void)RD;
Ted Kremenekfa417142009-08-06 01:20:57 +00001320#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001321 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1322
Ted Kremenek609df302009-06-17 22:02:04 +00001323 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1324 // reverse iterator, we should implement one.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001325 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor91f84212008-12-11 16:49:14 +00001326
Douglas Gregor7a4fad12008-12-11 20:41:00 +00001327 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1328 FieldEnd = Fields.rend();
1329 Field != FieldEnd; ++Field) {
1330 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001331 QualType FTy = (*Field)->getType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001332 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001333 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1334 }
1335
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001336 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001337#else
1338 return ValMgr.makeLazyCompoundVal(state, R);
1339#endif
Zhongxing Xu6c0d5882008-10-31 07:16:08 +00001340}
1341
Ted Kremenek609df302009-06-17 22:02:04 +00001342SVal RegionStoreManager::RetrieveArray(const GRState *state,
1343 const TypedRegion * R) {
Ted Kremenekfa417142009-08-06 01:20:57 +00001344#if USE_EXPLICIT_COMPOUND
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001345 QualType T = R->getValueType(getContext());
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001346 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1347
1348 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001349 uint64_t size = CAT->getSize().getZExtValue();
1350 for (uint64_t i = 0; i < size; ++i) {
1351 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu838a0db2009-06-16 09:55:50 +00001352 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump11289f42009-09-09 15:08:12 +00001353 getContext());
Ted Kremenek02e50892009-05-04 06:18:28 +00001354 QualType ETy = ER->getElementType();
Ted Kremenekac7c7242009-07-21 21:03:30 +00001355 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001356 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1357 }
1358
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001359 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremenekfa417142009-08-06 01:20:57 +00001360#else
1361 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1362 return ValMgr.makeLazyCompoundVal(state, R);
1363#endif
Zhongxing Xu3e3e69b2009-05-03 00:27:40 +00001364}
1365
Ted Kremenek4533a552009-06-16 22:36:44 +00001366//===----------------------------------------------------------------------===//
1367// Binding values to regions.
1368//===----------------------------------------------------------------------===//
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001369
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001370Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001371 const MemRegion* R = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001372
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001373 if (isa<loc::MemRegionVal>(L))
1374 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001375
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001376 if (R) {
Mike Stump11289f42009-09-09 15:08:12 +00001377 RegionBindings B = GetRegionBindings(store);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001378 return RBFactory.Remove(B, R).getRoot();
1379 }
Mike Stump11289f42009-09-09 15:08:12 +00001380
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001381 return store;
Zhongxing Xuc4a4c5f2008-12-16 02:36:30 +00001382}
1383
Ted Kremenek609df302009-06-17 22:02:04 +00001384const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xud260db12009-06-28 10:16:11 +00001385 if (isa<loc::ConcreteInt>(L))
1386 return state;
1387
Ted Kremenek4533a552009-06-16 22:36:44 +00001388 // If we get here, the location should be a region.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001389 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump11289f42009-09-09 15:08:12 +00001390
Ted Kremenek4533a552009-06-16 22:36:44 +00001391 // Check if the region is a struct region.
1392 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1393 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001394 return BindStruct(state, TR, V);
Mike Stump11289f42009-09-09 15:08:12 +00001395
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001396 // Special case: the current region represents a cast and it and the super
1397 // region both have pointer types or intptr_t types. If so, perform the
1398 // bind to the super region.
1399 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump11289f42009-09-09 15:08:12 +00001400 // loads that treat integers as pointers and vis versa.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001401 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1402 if (ER->getIndex().isZeroConstant()) {
1403 if (const TypedRegion *superR =
1404 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1405 ASTContext &Ctx = getContext();
1406 QualType superTy = superR->getValueType(Ctx);
1407 QualType erTy = ER->getValueType(Ctx);
Mike Stump11289f42009-09-09 15:08:12 +00001408
1409 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001410 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump11289f42009-09-09 15:08:12 +00001411 SValuator::CastResult cr =
1412 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001413 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1414 }
Ted Kremenek25c9c142009-09-21 22:58:52 +00001415 // For now, just invalidate the fields of the struct/union/class.
1416 // FIXME: Precisely handle the fields of the record.
1417 if (superTy->isRecordType())
Ted Kremenek1eb68092009-10-16 00:30:49 +00001418 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001419 }
1420 }
1421 }
Ted Kremenek267e45a2009-09-24 04:11:44 +00001422 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1423 // Binding directly to a symbolic region should be treated as binding
1424 // to element 0.
1425 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek1b40e592009-09-24 06:24:32 +00001426 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek267e45a2009-09-24 04:11:44 +00001427 R = GetElementZeroRegion(SR, T);
1428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001430 // Perform the binding.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001431 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001432 return state->makeWithStore(
1433 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek4533a552009-06-16 22:36:44 +00001434}
1435
Ted Kremenek14536f62009-08-21 22:28:32 +00001436const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekb006b822009-11-04 00:09:15 +00001437 const VarRegion *VR,
Ted Kremenek14536f62009-08-21 22:28:32 +00001438 SVal InitVal) {
Zhongxing Xu29188c22008-11-13 08:41:36 +00001439
Ted Kremenekb006b822009-11-04 00:09:15 +00001440 QualType T = VR->getDecl()->getType();
Zhongxing Xuce716382008-10-31 08:10:01 +00001441
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001442 if (T->isArrayType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001443 return BindArray(ST, VR, InitVal);
Ted Kremenekfe32cc02009-01-21 06:57:53 +00001444 if (T->isStructureType())
Ted Kremenek14536f62009-08-21 22:28:32 +00001445 return BindStruct(ST, VR, InitVal);
Zhongxing Xu2e8e6042008-11-02 12:13:30 +00001446
Ted Kremenek14536f62009-08-21 22:28:32 +00001447 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xud9959ae2008-10-08 02:50:44 +00001448}
Zhongxing Xu83aff702008-10-21 05:29:26 +00001449
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001450// FIXME: this method should be merged into Bind().
Ted Kremenek609df302009-06-17 22:02:04 +00001451const GRState *
1452RegionStoreManager::BindCompoundLiteral(const GRState *state,
1453 const CompoundLiteralExpr* CL,
1454 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001455
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001456 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
Ted Kremenek609df302009-06-17 22:02:04 +00001457 return Bind(state, loc::MemRegionVal(R), V);
Zhongxing Xu2c677c32008-11-07 10:38:33 +00001458}
1459
Ted Kremenek439a6d12009-11-19 20:20:24 +00001460const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1461 const MemRegion *R,
1462 QualType T) {
1463 Store store = state->getStore();
1464 RegionBindings B = GetRegionBindings(store);
1465 SVal V;
1466
1467 if (Loc::IsLocType(T))
1468 V = ValMgr.makeNull();
1469 else if (T->isIntegerType())
1470 V = ValMgr.makeZeroVal(T);
1471 else if (T->isStructureType() || T->isArrayType()) {
1472 // Set the default value to a zero constant when it is a structure
1473 // or array. The type doesn't really matter.
1474 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1475 }
1476 else {
1477 return state;
1478 }
1479
1480 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1481 return state->makeWithStore(B.getRoot());
1482}
1483
Ted Kremenek609df302009-06-17 22:02:04 +00001484const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001485 const TypedRegion* R,
Ted Kremenek609df302009-06-17 22:02:04 +00001486 SVal Init) {
1487
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001488 QualType T = R->getValueType(getContext());
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001489 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001490 QualType ElementTy = CAT->getElementType();
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001491
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001492 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001493
1494 // Check if the init expr is a StringLiteral.
1495 if (isa<loc::MemRegionVal>(Init)) {
1496 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1497 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1498 const char* str = S->getStrData();
1499 unsigned len = S->getByteLength();
1500 unsigned j = 0;
1501
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001502 // Copy bytes from the string literal into the target array. Trailing bytes
1503 // in the array that are not covered by the string literal are initialized
1504 // to zero.
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001505 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001506 if (j >= len)
1507 break;
1508
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001509 SVal Idx = ValMgr.makeArrayIndex(i);
1510 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1511 getContext());
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001512
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001513 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek609df302009-06-17 22:02:04 +00001514 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001515 }
1516
Ted Kremenek609df302009-06-17 22:02:04 +00001517 return state;
Zhongxing Xud2fa1e02008-11-30 05:49:49 +00001518 }
1519
Ted Kremenekfa417142009-08-06 01:20:57 +00001520 // Handle lazy compound values.
1521 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1522 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001523
1524 // Remaining case: explicit compound values.
Ted Kremenek439a6d12009-11-19 20:20:24 +00001525
1526 if (Init.isUnknown())
1527 return setImplicitDefaultValue(state, R, ElementTy);
1528
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001529 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001530 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001531 uint64_t i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001532
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001533 for (; i < size; ++i, ++VI) {
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001534 // The init list might be shorter than the array length.
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001535 if (VI == VE)
1536 break;
1537
Ted Kremenekc7b1dad2009-07-16 01:33:37 +00001538 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001539 ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001540
1541 if (CAT->getElementType()->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001542 state = BindStruct(state, ER, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001543 else
Ted Kremenek30030012009-09-22 21:19:14 +00001544 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001545 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001546 }
1547
Ted Kremenek439a6d12009-11-19 20:20:24 +00001548 // If the init list is shorter than the array length, set the
1549 // array default value.
1550 if (i < size)
1551 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xub7cf9592009-06-23 05:23:38 +00001552
Ted Kremenek609df302009-06-17 22:02:04 +00001553 return state;
Zhongxing Xu98bb1fa2008-10-31 10:24:47 +00001554}
1555
Ted Kremenek609df302009-06-17 22:02:04 +00001556const GRState *
1557RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1558 SVal V) {
Mike Stump11289f42009-09-09 15:08:12 +00001559
Ted Kremenek609df302009-06-17 22:02:04 +00001560 if (!Features.supportsFields())
1561 return state;
Mike Stump11289f42009-09-09 15:08:12 +00001562
Zhongxing Xu34d04b32009-05-09 03:57:34 +00001563 QualType T = R->getValueType(getContext());
Zhongxing Xub393b502008-10-31 10:53:01 +00001564 assert(T->isStructureType());
1565
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001566 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xub393b502008-10-31 10:53:01 +00001567 RecordDecl* RD = RT->getDecl();
Zhongxing Xud2e89ae2009-03-11 09:07:35 +00001568
1569 if (!RD->isDefinition())
Ted Kremenek609df302009-06-17 22:02:04 +00001570 return state;
Zhongxing Xub393b502008-10-31 10:53:01 +00001571
Ted Kremenekfa417142009-08-06 01:20:57 +00001572 // Handle lazy compound values.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001573 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremenekfa417142009-08-06 01:20:57 +00001574 return CopyLazyBindings(*LCV, state, R);
Mike Stump11289f42009-09-09 15:08:12 +00001575
Ted Kremenek609df302009-06-17 22:02:04 +00001576 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1577 // Ignore them and kill the field values.
1578 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001579 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu519a47d2009-06-11 09:11:27 +00001580
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001581 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xub393b502008-10-31 10:53:01 +00001582 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xu0442e962009-06-23 05:43:16 +00001583
1584 RecordDecl::field_iterator FI, FE;
1585
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001586 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001587
Zhongxing Xu0442e962009-06-23 05:43:16 +00001588 if (VI == VE)
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001589 break;
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001590
Zhongxing Xub393b502008-10-31 10:53:01 +00001591 QualType FTy = (*FI)->getType();
Ted Kremenek30030012009-09-22 21:19:14 +00001592 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xub393b502008-10-31 10:53:01 +00001593
Ted Kremenek30030012009-09-22 21:19:14 +00001594 if (FTy->isArrayType())
Ted Kremenek609df302009-06-17 22:02:04 +00001595 state = BindArray(state, FR, *VI);
Zhongxing Xuaf7415f2008-12-20 06:32:12 +00001596 else if (FTy->isStructureType())
Ted Kremenek609df302009-06-17 22:02:04 +00001597 state = BindStruct(state, FR, *VI);
Ted Kremenek30030012009-09-22 21:19:14 +00001598 else
1599 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xu729518b2008-10-24 08:42:28 +00001600 }
1601
Zhongxing Xu0442e962009-06-23 05:43:16 +00001602 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001603 if (FI != FE) {
1604 Store store = state->getStore();
1605 RegionBindings B = GetRegionBindings(store);
1606 B = RBFactory.Add(B, R,
1607 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1608 state = state->makeWithStore(B.getRoot());
1609 }
Zhongxing Xu0442e962009-06-23 05:43:16 +00001610
Ted Kremenek609df302009-06-17 22:02:04 +00001611 return state;
Zhongxing Xue5816f22008-11-19 11:06:24 +00001612}
1613
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001614Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1615 RegionBindings B = GetRegionBindings(store);
1616 llvm::OwningPtr<RegionStoreSubRegionMap>
1617 SubRegions(getRegionStoreSubRegionMap(store));
1618 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xucff637a2009-01-13 01:49:57 +00001619
Zhongxing Xuc53b4442009-06-25 05:52:16 +00001620 // Set the default value of the struct region to "unknown".
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001621 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xucff637a2009-01-13 01:49:57 +00001622
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001623 return B.getRoot();
Zhongxing Xucff637a2009-01-13 01:49:57 +00001624}
1625
Ted Kremenekfa417142009-08-06 01:20:57 +00001626const GRState*
1627RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1628 const GRState *state,
1629 const TypedRegion *R) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001630
Ted Kremenekfa417142009-08-06 01:20:57 +00001631 // Nuke the old bindings stemming from R.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001632 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenekfa417142009-08-06 01:20:57 +00001633
Mike Stump11289f42009-09-09 15:08:12 +00001634 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001635 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenekfa417142009-08-06 01:20:57 +00001636
Mike Stump11289f42009-09-09 15:08:12 +00001637 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001638 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump11289f42009-09-09 15:08:12 +00001639
Ted Kremenekfa417142009-08-06 01:20:57 +00001640 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1641 // results in a zero-copy algorithm.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001642 return state->makeWithStore(
1643 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenekfa417142009-08-06 01:20:57 +00001644}
Mike Stump11289f42009-09-09 15:08:12 +00001645
Ted Kremenek4533a552009-06-16 22:36:44 +00001646//===----------------------------------------------------------------------===//
1647// State pruning.
1648//===----------------------------------------------------------------------===//
Ted Kremenekcc224242009-09-29 06:35:00 +00001649
Mike Stump11289f42009-09-09 15:08:12 +00001650void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenekcee28a42009-08-02 04:45:08 +00001651 SymbolReaper& SymReaper,
Ted Kremenek4533a552009-06-16 22:36:44 +00001652 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump11289f42009-09-09 15:08:12 +00001653{
Ted Kremenek9f3a6432009-10-17 17:45:11 +00001654 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1655
Ted Kremenekcee28a42009-08-02 04:45:08 +00001656 Store store = state.getStore();
Ted Kremenek2c85f172009-08-06 04:50:20 +00001657 RegionBindings B = GetRegionBindings(store);
Mike Stump11289f42009-09-09 15:08:12 +00001658
Ted Kremenek4533a552009-06-16 22:36:44 +00001659 // The backmap from regions to subregions.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001660 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001661 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenekcc224242009-09-29 06:35:00 +00001662
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001663 // Do a pass over the regions in the store. For VarRegions we check if
1664 // the variable is still live and if so add it to the list of live roots.
1665 // For other regions we populate our region backmap.
Ted Kremenek4533a552009-06-16 22:36:44 +00001666 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenekcc224242009-09-29 06:35:00 +00001667
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001668 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001669 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001670 const MemRegion *R = I.getKey();
1671 IntermediateRoots.push_back(R);
Ted Kremenek4533a552009-06-16 22:36:44 +00001672 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001673
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001674 // Process the "intermediate" roots to find if they are referenced by
Mike Stump11289f42009-09-09 15:08:12 +00001675 // real roots.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001676 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001677 llvm::SmallVector<RBDNode, 10> Postponed;
1678
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001679 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001680
Ted Kremenek4533a552009-06-16 22:36:44 +00001681 while (!IntermediateRoots.empty()) {
1682 const MemRegion* R = IntermediateRoots.back();
1683 IntermediateRoots.pop_back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001684
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001685 if (IntermediateVisited.count(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001686 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001687 IntermediateVisited.insert(R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001688
Ted Kremenek4533a552009-06-16 22:36:44 +00001689 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001690 if (SymReaper.isLive(Loc, VR->getDecl()))
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001691 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001692 continue;
1693 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001694
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001695 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001696 llvm::SmallVectorImpl<RBDNode> &Q =
1697 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1698
1699 Q.push_back(std::make_pair(&state, SR));
1700
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001701 continue;
Ted Kremenek4533a552009-06-16 22:36:44 +00001702 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001703
1704 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001705 if (const SubRegion* superR =
Ted Kremenekcc224242009-09-29 06:35:00 +00001706 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek1f22aa72009-08-01 06:17:29 +00001707 IntermediateRoots.push_back(superR);
Ted Kremenek4533a552009-06-16 22:36:44 +00001708 }
Mike Stump11289f42009-09-09 15:08:12 +00001709
Ted Kremenekcc224242009-09-29 06:35:00 +00001710 // Enqueue the RegionRoots onto WorkList.
1711 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1712 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001713 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump11289f42009-09-09 15:08:12 +00001714 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001715 RegionRoots.clear();
1716
Zhongxing Xu775a2c02009-10-18 04:15:47 +00001717 llvm::DenseSet<RBDNode> Visited;
Ted Kremenekcc224242009-09-29 06:35:00 +00001718
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001719tryAgain:
Ted Kremenekcc224242009-09-29 06:35:00 +00001720 while (!WorkList.empty()) {
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001721 RBDNode N = WorkList.back();
Ted Kremenekcc224242009-09-29 06:35:00 +00001722 WorkList.pop_back();
1723
1724 // Have we visited this node before?
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001725 if (Visited.count(N))
Ted Kremenekcc224242009-09-29 06:35:00 +00001726 continue;
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001727 Visited.insert(N);
Mike Stump11289f42009-09-09 15:08:12 +00001728
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001729 const MemRegion *R = N.second;
1730 const GRState *state_N = N.first;
Ted Kremenekcc224242009-09-29 06:35:00 +00001731
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001732 // Enqueue subregions.
1733 RegionStoreSubRegionMap *M;
1734
1735 if (&state == state_N)
1736 M = SubRegions.get();
1737 else {
1738 RegionStoreSubRegionMap *& SM = SC[state_N];
1739 if (!SM)
1740 SM = getRegionStoreSubRegionMap(state_N->getStore());
1741 M = SM;
1742 }
1743
1744 RegionStoreSubRegionMap::iterator I, E;
1745 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1746 WorkList.push_back(std::make_pair(state_N, *I));
1747
Ted Kremenekcc224242009-09-29 06:35:00 +00001748 // Enqueue the super region.
1749 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1750 const MemRegion *superR = SR->getSuperRegion();
1751 if (!isa<MemSpaceRegion>(superR)) {
1752 // If 'R' is a field or an element, we want to keep the bindings
1753 // for the other fields and elements around. The reason is that
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001754 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8b2f5d32009-10-17 07:32:08 +00001755 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1756 || isa<ObjCIvarRegion>(R));
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001757 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenekcc224242009-09-29 06:35:00 +00001758 }
1759 }
1760
1761 // Mark the symbol for any live SymbolicRegion as "live". This means we
1762 // should continue to track that symbol.
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001763 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenekcc224242009-09-29 06:35:00 +00001764 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek94f8c4a2009-11-26 02:35:42 +00001765
1766 // For BlockDataRegions, enqueue all VarRegions for that are referenced
1767 // via BlockDeclRefExprs.
1768 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1769 for (BlockDataRegion::referenced_vars_iterator
1770 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
1771 RI != RE; ++RI)
1772 WorkList.push_back(std::make_pair(state_N, *RI));
1773
1774 // No possible data bindings on a BlockDataRegion. Continue to the
1775 // next region in the worklist.
1776 continue;
1777 }
Ted Kremenekcc224242009-09-29 06:35:00 +00001778
1779 Store store_N = state_N->getStore();
1780 RegionBindings B_N = GetRegionBindings(store_N);
1781
1782 // Get the data binding for R (if any).
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001783 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenekcc224242009-09-29 06:35:00 +00001784
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001785 if (V) {
1786 // Check for lazy bindings.
1787 if (const nonloc::LazyCompoundVal *LCV =
1788 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenekcc224242009-09-29 06:35:00 +00001789
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001790 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001791 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001792 }
1793 else {
Ted Kremenekcc224242009-09-29 06:35:00 +00001794 // Update the set of live symbols.
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001795 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenekcc224242009-09-29 06:35:00 +00001796 SI!=SE;++SI)
1797 SymReaper.markLive(*SI);
1798
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001799 // If V is a region, then add it to the worklist.
1800 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001801 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenekcc224242009-09-29 06:35:00 +00001802 }
1803 }
1804 }
1805
Ted Kremenek1f0a56e2009-10-29 05:14:17 +00001806 // See if any postponed SymbolicRegions are actually live now, after
1807 // having done a scan.
1808 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1809 E = Postponed.end() ; I != E ; ++I) {
1810 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1811 if (SymReaper.isLive(SR->getSymbol())) {
1812 WorkList.push_back(*I);
1813 I->second = NULL;
1814 }
1815 }
1816 }
1817
1818 if (!WorkList.empty())
1819 goto tryAgain;
1820
Ted Kremenek4533a552009-06-16 22:36:44 +00001821 // We have now scanned the store, marking reachable regions and symbols
1822 // as live. We now remove all the regions that are dead from the store
Mike Stump11289f42009-09-09 15:08:12 +00001823 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek2c85f172009-08-06 04:50:20 +00001824 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek4533a552009-06-16 22:36:44 +00001825 const MemRegion* R = I.getKey();
Ted Kremenek4533a552009-06-16 22:36:44 +00001826 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuc0c65082009-10-17 08:39:24 +00001827 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek4533a552009-06-16 22:36:44 +00001828 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001829
Ted Kremenek4533a552009-06-16 22:36:44 +00001830 // Remove this dead region from the store.
Zhongxing Xu7718ae42009-06-23 09:02:15 +00001831 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump11289f42009-09-09 15:08:12 +00001832
Ted Kremenek4533a552009-06-16 22:36:44 +00001833 // Mark all non-live symbols that this region references as dead.
1834 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1835 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00001836
Zhongxing Xub8edf2a2009-10-11 08:08:02 +00001837 SVal X = *I.getData().getValue();
Ted Kremenekf106ab92009-08-02 05:00:15 +00001838 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1839 for (; SI != SE; ++SI)
1840 SymReaper.maybeDead(*SI);
1841 }
Mike Stump11289f42009-09-09 15:08:12 +00001842
Ted Kremenekcee28a42009-08-02 04:45:08 +00001843 // Write the store back.
1844 state.setStore(store);
Ted Kremenek4533a552009-06-16 22:36:44 +00001845}
1846
Zhongxing Xudaa41762009-10-13 02:24:55 +00001847GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1848 StackFrameContext const *frame) {
1849 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1850 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1851
1852 FunctionDecl::param_const_iterator PI = FD->param_begin();
1853
1854 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1855
1856 // Copy the arg expression value to the arg variables.
1857 for (; AI != AE; ++AI, ++PI) {
1858 SVal ArgVal = state->getSVal(*AI);
1859 MemRegion *R = MRMgr.getVarRegion(*PI, frame);
1860 state = Bind(state, ValMgr.makeLoc(R), ArgVal);
1861 }
1862
1863 return state;
1864}
1865
Ted Kremenek4533a552009-06-16 22:36:44 +00001866//===----------------------------------------------------------------------===//
1867// Utility methods.
1868//===----------------------------------------------------------------------===//
1869
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001870void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek4533a552009-06-16 22:36:44 +00001871 const char* nl, const char *sep) {
Ted Kremenek2c85f172009-08-06 04:50:20 +00001872 RegionBindings B = GetRegionBindings(store);
Ted Kremenek481c1212009-10-20 01:20:57 +00001873 OS << "Store (direct and default bindings):" << nl;
Mike Stump11289f42009-09-09 15:08:12 +00001874
Ted Kremenek2c85f172009-08-06 04:50:20 +00001875 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001876 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek4533a552009-06-16 22:36:44 +00001877}