blob: c718f1fbba23372a84a8f6dcccad73cdfdbcdde1 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
Zhongxing Xu0b143312009-08-21 13:25:15 +000018#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000019#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000020#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000024
25#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000027#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000028
29using namespace clang;
30
Ted Kremenek356e9d62009-07-22 04:35:42 +000031#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000033
Zhongxing Xu13d50172009-10-11 08:08:02 +000034namespace {
35class BindingVal {
36public:
37 enum BindingKind { Direct, Default };
38private:
39 SVal Value;
40 BindingKind Kind;
41
42public:
43 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
44
45 bool isDefault() const { return Kind == Default; }
46
47 const SVal *getValue() const { return &Value; }
48
49 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
50
51 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
52
53 void Profile(llvm::FoldingSetNodeID& ID) const {
54 Value.Profile(ID);
55 ID.AddInteger(Kind);
56 }
57
58 inline bool operator==(const BindingVal& R) const {
59 return Value == R.Value && Kind == R.Kind;
60 }
61
62 inline bool operator!=(const BindingVal& R) const {
63 return !(*this == R);
64 }
65};
66}
67
68namespace llvm {
69static inline
70llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
71 if (V.isDefault())
72 os << "(default) ";
73 else
74 os << "(direct) ";
75 os << *V.getValue();
76 return os;
77}
78} // end llvm namespace
79
Zhongxing Xubaf03a72008-11-24 09:44:56 +000080// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000081typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000082
Ted Kremenek50dc1b32008-12-24 01:05:03 +000083//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000084// Fine-grained control of RegionStoreManager.
85//===----------------------------------------------------------------------===//
86
87namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000088struct minimal_features_tag {};
89struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000090
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000091class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +000092 bool SupportsFields;
93 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Ted Kremenek9af46f52009-06-16 22:36:44 +000095public:
96 RegionStoreFeatures(minimal_features_tag) :
97 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek9af46f52009-06-16 22:36:44 +000099 RegionStoreFeatures(maximal_features_tag) :
100 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek9af46f52009-06-16 22:36:44 +0000102 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek9af46f52009-06-16 22:36:44 +0000104 bool supportsFields() const { return SupportsFields; }
105 bool supportsRemaining() const { return SupportsRemaining; }
106};
107}
108
109//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110// Region "Extents"
111//===----------------------------------------------------------------------===//
112//
113// MemRegions represent chunks of memory with a size (their "extent"). This
114// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000115//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000116namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000118namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000119 template<> struct GRStateTrait<RegionExtents>
120 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
121 static void* GDMIndex() { return &RegionExtentsIndex; }
122 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000123}
124
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000125//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000126// Utility functions.
127//===----------------------------------------------------------------------===//
128
129static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
130 if (ty->isAnyPointerType())
131 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000133 return ty->isIntegerType() && ty->isScalarType() &&
134 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
135}
136
137//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000138// Main RegionStore logic.
139//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000140
Zhongxing Xu17892752008-10-08 02:50:44 +0000141namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000143class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000145 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000146 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000149 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000150 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000151
152 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000153 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000154 return true;
155 }
156
157 I->second = F.Add(I->second, SubRegion);
158 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000161 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek5dc27462009-03-03 02:51:43 +0000165 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000166 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167
168 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000169 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek59e8f112009-03-03 01:35:36 +0000171 llvm::ImmutableSet<const MemRegion*> S = I->second;
172 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
173 SI != SE; ++SI) {
174 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000175 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek5dc27462009-03-03 02:51:43 +0000178 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000181 typedef SetTy::iterator iterator;
182
183 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
184 Map::iterator I = M.find(R);
185 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
186 return std::make_pair(S.begin(), S.end());
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000189
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000190class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000191 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000192 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000193
194 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
195 SMCache SC;
196
Zhongxing Xu17892752008-10-08 02:50:44 +0000197public:
Mike Stump1eb44332009-09-09 15:08:12 +0000198 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000199 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000200 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000201 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000202
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000203 virtual ~RegionStoreManager() {
204 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
205 delete (*I).second;
206 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000207
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000208 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Zhongxing Xu13d50172009-10-11 08:08:02 +0000210 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Zhongxing Xu13d50172009-10-11 08:08:02 +0000212 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
213 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000214 /// getDefaultBinding - Returns an SVal* representing an optional default
215 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000216 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000217
218 /// setImplicitDefaultValue - Set the default binding for the provided
219 /// MemRegion to the value implicitly defined for compound literals when
220 /// the value is not specified.
221 const GRState *setImplicitDefaultValue(const GRState *state,
222 const MemRegion *R,
223 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000225 /// getLValueString - Returns an SVal representing the lvalue of a
226 /// StringLiteral. Within RegionStore a StringLiteral has an
227 /// associated StringRegion, and the lvalue of a StringLiteral is
228 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000229 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000230
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000231 /// getLValueCompoundLiteral - Returns an SVal representing the
232 /// lvalue of a compound literal. Within RegionStore a compound
233 /// literal has an associated region, and the lvalue of the
234 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000235 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000236
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000237 /// getLValueVar - Returns an SVal that represents the lvalue of a
238 /// variable. Within RegionStore a variable has an associated
239 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000240 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000242 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000243
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000244 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000246 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000247
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000248 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000249
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000250
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000251 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
252 /// type. 'Array' represents the lvalue of the array being decayed
253 /// to a pointer, and the returned SVal represents the decayed
254 /// version of that lvalue (i.e., a pointer to the first element of
255 /// the array). This is called by GRExprEngine when evaluating
256 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000257 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000258
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000259 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000260 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000261
Mike Stump1eb44332009-09-09 15:08:12 +0000262 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000263 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000264 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 //===-------------------------------------------------------------------===//
267 // Binding values to regions.
268 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000269
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000270 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000271 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-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 Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000282private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000283 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000284 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000287 const GRState *Bind(const GRState *state, Loc LV, SVal V);
288
289 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +0000290 const CompoundLiteralExpr* CL,
291 const LocationContext *LC,
292 SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000294 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
295 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000296
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000297 const GRState *BindDeclWithNoInit(const GRState *state,
298 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000299 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000300 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000301
Ted Kremenek67f28532009-06-17 22:02:04 +0000302 /// BindStruct - Bind a compound value to a structure.
303 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
Ted Kremenek67f28532009-06-17 22:02:04 +0000305 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000306
307 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000308 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000309
Ted Kremenek67f28532009-06-17 22:02:04 +0000310 Store Remove(Store store, Loc LV);
311
312 //===------------------------------------------------------------------===//
313 // Loading values from regions.
314 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Ted Kremenek67f28532009-06-17 22:02:04 +0000316 /// The high level logic for this method is this:
317 /// Retrieve (L)
318 /// if L has binding
319 /// return L's binding
320 /// else if L is in killset
321 /// return unknown
322 /// else
323 /// if L is on stack or heap
324 /// return undefined
325 /// else
326 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000327 SValuator::CastResult Retrieve(const GRState *state, Loc L,
328 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000329
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000330 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000331
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000332 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000334 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek9031dd72009-07-21 00:12:07 +0000336 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek25c54572009-07-20 22:58:02 +0000338 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000340 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
341 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Ted Kremenek67f28532009-06-17 22:02:04 +0000343 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000344 /// struct copy:
345 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000346 /// x = y;
347 /// y's value is retrieved by this method.
348 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremenek67f28532009-06-17 22:02:04 +0000350 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000352 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000353 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000355 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
356 const GRState *state,
357 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000358
Ted Kremenek0954cde2009-09-24 04:11:44 +0000359 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
360 QualType T);
361
Ted Kremenek67f28532009-06-17 22:02:04 +0000362 //===------------------------------------------------------------------===//
363 // State pruning.
364 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Ted Kremenek67f28532009-06-17 22:02:04 +0000366 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
367 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000368 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000369 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
370
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000371 const GRState *EnterStackFrame(const GRState *state,
372 const StackFrameContext *frame);
373
Ted Kremenek67f28532009-06-17 22:02:04 +0000374 //===------------------------------------------------------------------===//
375 // Region "extents".
376 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Ted Kremenek67f28532009-06-17 22:02:04 +0000378 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000379 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
380 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000381
382 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000383 // Utility methods.
384 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Ted Kremenek451ac092009-08-06 04:50:20 +0000386 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000387 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000388 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000389
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000390 void print(Store store, llvm::raw_ostream& Out, const char* nl,
391 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000392
393 void iterBindings(Store store, BindingsHandler& f) {
394 // FIXME: Implement.
395 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000396
Ted Kremenek67f28532009-06-17 22:02:04 +0000397 // FIXME: Remove.
398 BasicValueFactory& getBasicVals() {
399 return StateMgr.getBasicVals();
400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Ted Kremenek67f28532009-06-17 22:02:04 +0000402 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000403 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000404};
405
406} // end anonymous namespace
407
Ted Kremenek9af46f52009-06-16 22:36:44 +0000408//===----------------------------------------------------------------------===//
409// RegionStore creation.
410//===----------------------------------------------------------------------===//
411
412StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
413 RegionStoreFeatures F = maximal_features_tag();
414 return new RegionStoreManager(StMgr, F);
415}
416
417StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
418 RegionStoreFeatures F = minimal_features_tag();
419 F.enableFields(true);
420 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000421}
422
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000423void
424RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000425 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000426 const MemRegion *superR = R->getSuperRegion();
427 if (add(superR, R))
428 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000429 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000430}
431
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000432RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000433RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
434 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000435 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000437 llvm::SmallVector<const SubRegion*, 10> WL;
438
Ted Kremenek451ac092009-08-06 04:50:20 +0000439 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000440 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
441 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Mike Stump1eb44332009-09-09 15:08:12 +0000443 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000444 // don't have direct bindings but are super regions of those that do.
445 while (!WL.empty()) {
446 const SubRegion *R = WL.back();
447 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000448 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000449 }
450
Ted Kremenek14453bf2009-03-03 19:02:42 +0000451 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000452}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000453
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000454SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000455 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000456}
457
Ted Kremenek9af46f52009-06-16 22:36:44 +0000458//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000459// Binding invalidation.
460//===----------------------------------------------------------------------===//
461
Zhongxing Xu13d50172009-10-11 08:08:02 +0000462void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
463 const MemRegion *R,
464 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000465 RegionStoreSubRegionMap::iterator I, E;
466
467 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000468 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000470 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000471}
472
Ted Kremenek81a95832009-12-03 03:27:11 +0000473const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
474 const MemRegion * const *I,
475 const MemRegion * const *E,
476 const Expr *Ex,
477 unsigned Count,
478 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000479 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000480
Ted Kremenek87806792009-09-27 20:45:21 +0000481 // Get the mapping of regions -> subregions.
482 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000483 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000484
485 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000486
Ted Kremenek87806792009-09-27 20:45:21 +0000487 llvm::DenseMap<const MemRegion *, unsigned> Visited;
488 llvm::SmallVector<const MemRegion *, 10> WorkList;
Ted Kremenek81a95832009-12-03 03:27:11 +0000489
490 for ( ; I != E; ++I) {
491 // Strip away casts.
492 WorkList.push_back((*I)->StripCasts());
493 }
Ted Kremenek87806792009-09-27 20:45:21 +0000494
495 while (!WorkList.empty()) {
Ted Kremenek81a95832009-12-03 03:27:11 +0000496 const MemRegion *R = WorkList.back();
Ted Kremenek87806792009-09-27 20:45:21 +0000497 WorkList.pop_back();
498
499 // Have we visited this region before?
500 unsigned &visited = Visited[R];
501 if (visited)
502 continue;
503 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Ted Kremenek87806792009-09-27 20:45:21 +0000505 // Add subregions to work list.
506 RegionStoreSubRegionMap::iterator I, E;
507 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
508 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000509
510 // Get the old binding. Is it a region? If so, add it to the worklist.
511 if (Optional<SVal> V = getDirectBinding(B, R)) {
512 if (const MemRegion *RV = V->getAsRegion())
513 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000514
515 // A symbol? Mark it touched by the invalidation.
516 if (IS) {
517 if (SymbolRef Sym = V->getAsSymbol())
518 IS->insert(Sym);
519 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000520 }
521
Ted Kremenek473e1672009-10-16 00:30:49 +0000522 // Symbolic region? Mark that symbol touched by the invalidation.
523 if (IS) {
524 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
525 IS->insert(SR->getSymbol());
526 }
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000527
528 // BlockDataRegion? If so, invalidate captured variables that are passed
529 // by reference.
530 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
531 for (BlockDataRegion::referenced_vars_iterator
532 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
533 I != E; ++I) {
534 const VarRegion *VR = *I;
535 if (VR->getDecl()->getAttr<BlocksAttr>())
536 WorkList.push_back(VR);
537 }
538 continue;
539 }
Ted Kremenek473e1672009-10-16 00:30:49 +0000540
541 // Handle the region itself.
Ted Kremenek87806792009-09-27 20:45:21 +0000542 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
543 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000544 // Invalidate the region by setting its default value to
545 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000546 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
547 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000548 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000549 continue;
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Ted Kremenek87806792009-09-27 20:45:21 +0000552 if (!R->isBoundable())
553 continue;
554
555 const TypedRegion *TR = cast<TypedRegion>(R);
556 QualType T = TR->getValueType(Ctx);
557
558 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000559 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
560
Zhongxing Xu13d50172009-10-11 08:08:02 +0000561 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000562 if (!RD)
563 continue;
564
Zhongxing Xu13d50172009-10-11 08:08:02 +0000565 // Invalidate the region by setting its default value to
566 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000567 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
568 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000569 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000570 continue;
571 }
572
573 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
574 // Set the default value of the array to conjured symbol.
575 DefinedOrUnknownSVal V =
576 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000577 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000578 continue;
579 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000580
581 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
582 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000583 // For fields and elements whose super region has also been invalidated,
584 // only remove the old binding. The super region will get set with a
585 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000586 B = RBFactory.Remove(B, R);
587 continue;
588 }
Ted Kremenek87806792009-09-27 20:45:21 +0000589
Ted Kremenek389c44c2009-09-29 03:12:50 +0000590 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000591 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
592 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000593 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000594 }
595
Ted Kremenek87806792009-09-27 20:45:21 +0000596 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000597 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000598}
599
600//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000601// getLValueXXX methods.
602//===----------------------------------------------------------------------===//
603
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000604/// getLValueString - Returns an SVal representing the lvalue of a
605/// StringLiteral. Within RegionStore a StringLiteral has an
606/// associated StringRegion, and the lvalue of a StringLiteral is the
607/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000608SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000609 return loc::MemRegionVal(MRMgr.getStringRegion(S));
610}
611
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000612/// getLValueVar - Returns an SVal that represents the lvalue of a
613/// variable. Within RegionStore a variable has an associated
614/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000615SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000616 const LocationContext *LC) {
617 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000618}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000619
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000620SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
621 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000622}
623
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000624SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
625 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000626}
627
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000628SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000629 if (Base.isUnknownOrUndef())
630 return Base;
631
632 Loc BaseL = cast<Loc>(Base);
633 const MemRegion* BaseR = 0;
634
635 switch (BaseL.getSubKind()) {
636 case loc::MemRegionKind:
637 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
638 break;
639
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000640 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000641 // These are anormal cases. Flag an undefined value.
642 return UndefinedVal();
643
644 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000645 // While these seem funny, this can happen through casts.
646 // FIXME: What we should return is the field offset. For example,
647 // add the field offset to the integer value. That way funny things
648 // like this work properly: &(((struct foo *) 0xa)->f)
649 return Base;
650
651 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000652 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000653 return Base;
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000656 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
657 // of FieldDecl.
658 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
659 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000660
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000661 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000662}
663
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000664SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
665 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000666
Ted Kremenekde7ec632009-03-09 22:44:49 +0000667 // If the base is an unknown or undefined value, just return it back.
668 // FIXME: For absolute pointer addresses, we just return that value back as
669 // well, although in reality we should return the offset added to that
670 // value.
671 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000672 return Base;
673
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000674 // Only handle integer offsets... for now.
675 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000676 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000677
Zhongxing Xuce760782009-05-09 13:20:07 +0000678 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000679
680 // Pointer of any type can be cast and used as array base.
681 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Ted Kremenek46537392009-07-16 01:33:37 +0000683 // Convert the offset to the appropriate size and signedness.
684 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000686 if (!ElemR) {
687 //
688 // If the base region is not an ElementRegion, create one.
689 // This can happen in the following example:
690 //
691 // char *p = __builtin_alloc(10);
692 // p[1] = 8;
693 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000694 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000695 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000696 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000697 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000698 }
Mike Stump1eb44332009-09-09 15:08:12 +0000699
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000700 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000702 if (!isa<nonloc::ConcreteInt>(BaseIdx))
703 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000705 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
706 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
707 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremenek46537392009-07-16 01:33:37 +0000709 // Compute the new index.
710 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremenek46537392009-07-16 01:33:37 +0000712 // Construct the new ElementRegion.
713 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000714 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000715 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000716}
717
Ted Kremenek9af46f52009-06-16 22:36:44 +0000718//===----------------------------------------------------------------------===//
719// Extents for regions.
720//===----------------------------------------------------------------------===//
721
Zhongxing Xue884ff82009-11-12 02:48:32 +0000722DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
723 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000725 switch (R->getKind()) {
Ted Kremenek67d12872009-12-07 22:05:27 +0000726 case MemRegion::GenericMemSpaceRegionKind:
727 case MemRegion::StackLocalsSpaceRegionKind:
728 case MemRegion::StackArgumentsSpaceRegionKind:
729 case MemRegion::HeapSpaceRegionKind:
730 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000731 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000732 return UnknownVal();
733
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000734 case MemRegion::FunctionTextRegionKind:
735 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000736 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000737 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000738 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000739
740 // Not yet handled.
741 case MemRegion::AllocaRegionKind:
742 case MemRegion::CompoundLiteralRegionKind:
743 case MemRegion::ElementRegionKind:
744 case MemRegion::FieldRegionKind:
745 case MemRegion::ObjCIvarRegionKind:
746 case MemRegion::ObjCObjectRegionKind:
747 case MemRegion::SymbolicRegionKind:
748 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000750 case MemRegion::StringRegionKind: {
751 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000752 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000753 // operations with signed indices.
754 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000757 case MemRegion::VarRegionKind: {
758 const VarRegion* VR = cast<VarRegion>(R);
759 // Get the type of the variable.
760 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000762 // FIXME: Handle variable-length arrays.
763 if (isa<VariableArrayType>(T))
764 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000766 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
767 // return the size as signed integer.
768 return ValMgr.makeIntVal(CAT->getSize(), false);
769 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000770
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000771 // Clients can use ordinary variables as if they were arrays. These
772 // essentially are arrays of size 1.
773 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000774 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000777 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000778 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000779}
780
Ted Kremenek67f28532009-06-17 22:02:04 +0000781const GRState *RegionStoreManager::setExtent(const GRState *state,
782 const MemRegion *region,
783 SVal extent) {
784 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000785}
786
787//===----------------------------------------------------------------------===//
788// Location and region casting.
789//===----------------------------------------------------------------------===//
790
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000791/// ArrayToPointer - Emulates the "decay" of an array to a pointer
792/// type. 'Array' represents the lvalue of the array being decayed
793/// to a pointer, and the returned SVal represents the decayed
794/// version of that lvalue (i.e., a pointer to the first element of
795/// the array). This is called by GRExprEngine when evaluating casts
796/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000797SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000798 if (!isa<loc::MemRegionVal>(Array))
799 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Ted Kremenekabb042f2008-12-13 19:24:37 +0000801 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
802 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000804 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000805 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000807 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000808 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000809 ArrayType *AT = cast<ArrayType>(T);
810 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Ted Kremenek75185b52009-07-16 00:00:11 +0000812 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000813 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
814 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000815}
816
Ted Kremenek9af46f52009-06-16 22:36:44 +0000817//===----------------------------------------------------------------------===//
818// Pointer arithmetic.
819//===----------------------------------------------------------------------===//
820
Mike Stump1eb44332009-09-09 15:08:12 +0000821SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000822 BinaryOperator::Opcode Op, Loc L, NonLoc R,
823 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000824 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000825 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000826 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000827
Zhongxing Xua1718c72009-04-03 07:33:13 +0000828 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000829 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000830
Ted Kremenek3bccf082009-07-11 00:58:27 +0000831 switch (MR->getKind()) {
832 case MemRegion::SymbolicRegionKind: {
833 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000834 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000835 QualType T = Sym->getType(getContext());
836 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000838 if (const PointerType *PT = T->getAs<PointerType>())
839 EleTy = PT->getPointeeType();
840 else
John McCall183700f2009-09-21 23:43:11 +0000841 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Ted Kremenek3bccf082009-07-11 00:58:27 +0000843 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
844 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000845 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000846 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000847 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000848 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000849 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000850 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000851 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
852 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000853 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000854 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000855
Ted Kremenek3bccf082009-07-11 00:58:27 +0000856 case MemRegion::ElementRegionKind: {
857 ER = cast<ElementRegion>(MR);
858 break;
859 }
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Ted Kremenek3bccf082009-07-11 00:58:27 +0000861 // Not yet handled.
862 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000863 case MemRegion::StringRegionKind: {
864
865 }
866 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000867 case MemRegion::CompoundLiteralRegionKind:
868 case MemRegion::FieldRegionKind:
869 case MemRegion::ObjCObjectRegionKind:
870 case MemRegion::ObjCIvarRegionKind:
871 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000873 case MemRegion::FunctionTextRegionKind:
874 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000875 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000876 // Technically this can happen if people do funny things with casts.
877 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Ted Kremenek67d12872009-12-07 22:05:27 +0000879 case MemRegion::GenericMemSpaceRegionKind:
880 case MemRegion::StackLocalsSpaceRegionKind:
881 case MemRegion::StackArgumentsSpaceRegionKind:
882 case MemRegion::HeapSpaceRegionKind:
883 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000884 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
885 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000886 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000887
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000888 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000889 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000890
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000891 // For now, only support:
892 // (a) concrete integer indices that can easily be resolved
893 // (b) 0 + symbolic index
894 if (Base) {
895 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
896 // FIXME: Should use SValuator here.
897 SVal NewIdx =
898 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000899 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000900 const MemRegion* NewER =
901 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
902 ER->getSuperRegion(), getContext());
903 return ValMgr.makeLoc(NewER);
904 }
905 if (0 == Base->getValue()) {
906 const MemRegion* NewER =
907 MRMgr.getElementRegion(ER->getElementType(), R,
908 ER->getSuperRegion(), getContext());
909 return ValMgr.makeLoc(NewER);
910 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Ted Kremenek5dc27462009-03-03 02:51:43 +0000913 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000914}
915
Ted Kremenek9af46f52009-06-16 22:36:44 +0000916//===----------------------------------------------------------------------===//
917// Loading values from regions.
918//===----------------------------------------------------------------------===//
919
Zhongxing Xu13d50172009-10-11 08:08:02 +0000920Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
921 const MemRegion *R) {
922 if (const BindingVal *BV = B.lookup(R))
923 return Optional<SVal>::create(BV->getDirectValue());
924
925 return Optional<SVal>();
926}
927
928Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000929 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000931 if (R->isBoundable())
932 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
933 if (TR->getValueType(getContext())->isUnionType())
934 return UnknownVal();
935
Zhongxing Xu13d50172009-10-11 08:08:02 +0000936 if (BindingVal const *V = B.lookup(R))
937 return Optional<SVal>::create(V->getDefaultValue());
938
939 return Optional<SVal>();
940}
941
942Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
943 const MemRegion *R) {
944 if (const BindingVal *BV = B.lookup(R))
945 return Optional<SVal>::create(BV->getValue());
946
947 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000948}
949
Ted Kremeneka6275a52009-07-15 02:31:43 +0000950static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
951 RTy = Ctx.getCanonicalType(RTy);
952 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Ted Kremeneka6275a52009-07-15 02:31:43 +0000954 if (RTy == UsedTy)
955 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
957
Ted Kremenek25c54572009-07-20 22:58:02 +0000958 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000959 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000960 // represents a reinterpretation.
961 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000962 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000963 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000964
965 return PUsedTy && PRTy &&
966 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000967 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000968 }
969
970 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000971}
972
Ted Kremenek0954cde2009-09-24 04:11:44 +0000973const ElementRegion *
974RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
975 ASTContext &Ctx = getContext();
976 SVal idx = ValMgr.makeZeroArrayIndex();
977 assert(!T.isNull());
978 return MRMgr.getElementRegion(T, idx, SR, Ctx);
979}
980
981
982
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000983SValuator::CastResult
984RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000985
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000986 assert(!isa<UnknownVal>(L) && "location unknown");
987 assert(!isa<UndefinedVal>(L) && "location undefined");
988
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000989 // FIXME: Is this even possible? Shouldn't this be treated as a null
990 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000991 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000992 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000993
Ted Kremenek67f28532009-06-17 22:02:04 +0000994 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +0000995
Zhongxing Xu91844122009-05-20 09:18:48 +0000996 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +0000997 // Example:
998 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +0000999 // char* p = alloca();
1000 // read(p);
1001 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001002 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001003 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001004
Ted Kremenek0954cde2009-09-24 04:11:44 +00001005 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1006 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Ted Kremenek968f0a62009-08-03 21:41:46 +00001008 if (isa<CodeTextRegion>(MR))
1009 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001011 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1012 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001013 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001014 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001015
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001016 // FIXME: We should eventually handle funny addressing. e.g.:
1017 //
1018 // int x = ...;
1019 // int *p = &x;
1020 // char *q = (char*) p;
1021 // char c = *q; // returns the first byte of 'x'.
1022 //
1023 // Such funny addressing will occur due to layering of regions.
1024
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001025#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001026 ASTContext &Ctx = getContext();
1027 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001028 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1029 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001030 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001031 assert(Ctx.getCanonicalType(RTy) ==
1032 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001033 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001034#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001035
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001036 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001037 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001039 // FIXME: Handle unions.
1040 if (RTy->isUnionType())
1041 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001042
1043 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001044 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001045
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001046 // FIXME: handle Vector types.
1047 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001048 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001049
1050 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001051 return SValuator::CastResult(state,
1052 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001053
1054 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001055 return SValuator::CastResult(state,
1056 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Ted Kremenek25c54572009-07-20 22:58:02 +00001058 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001059 return SValuator::CastResult(state,
1060 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Ted Kremenek9031dd72009-07-21 00:12:07 +00001062 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001063 return SValuator::CastResult(state,
1064 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001065
Ted Kremenek451ac092009-08-06 04:50:20 +00001066 RegionBindings B = GetRegionBindings(state->getStore());
1067 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001068
1069 // Check if the region has a binding.
1070 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001071 if (SVal const *SV = V->getValue())
1072 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001073
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001074 // The location does not have a bound value. This means that it has
1075 // the value it had upon its creation and/or entry to the analyzed
1076 // function/method. These are either symbolic values or 'undefined'.
1077
Ted Kremenek356e9d62009-07-22 04:35:42 +00001078#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001079 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001080#else
1081 if (R->hasStackStorage()) {
1082#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001083 // All stack variables are considered to have undefined values
1084 // upon creation. All heap allocated blocks are considered to
1085 // have undefined values as well unless they are explicitly bound
1086 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001087 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001088 }
1089
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001090 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001091 return SValuator::CastResult(state,
1092 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001093}
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001095std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001096RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001097 if (Optional<SVal> OV = getDirectBinding(B, R))
1098 if (const nonloc::LazyCompoundVal *V =
1099 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1100 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001102 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1103 const std::pair<const GRState *, const MemRegion *> &X =
1104 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001106 if (X.first)
1107 return std::make_pair(X.first,
1108 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001109 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001110 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1111 const std::pair<const GRState *, const MemRegion *> &X =
1112 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001114 if (X.first)
1115 return std::make_pair(X.first,
1116 MRMgr.getFieldRegionWithSuper(FR, X.second));
1117 }
1118
1119 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1120}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001121
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001122SVal RegionStoreManager::RetrieveElement(const GRState* state,
1123 const ElementRegion* R) {
1124 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001125 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001126 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001127 return *V;
1128
Ted Kremenek921109a2009-07-01 23:19:52 +00001129 const MemRegion* superR = R->getSuperRegion();
1130
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001131 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001132 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001133 // FIXME: Handle loads from strings where the literal is treated as
1134 // an integer, e.g., *((unsigned int*)"hello")
1135 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001136 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001137 if (T != Ctx.getCanonicalType(R->getElementType()))
1138 return UnknownVal();
1139
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001140 const StringLiteral *Str = StrR->getStringLiteral();
1141 SVal Idx = R->getIndex();
1142 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1143 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001144 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001145 if (i > byteLength) {
1146 // Buffer overflow checking in GRExprEngine should handle this case,
1147 // but we shouldn't rely on it to not overflow here if that checking
1148 // is disabled.
1149 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001150 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001151 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001152 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001153 }
1154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001156 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001157 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001158 if (SymbolRef parentSym = V->getAsSymbol())
1159 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001160
1161 if (V->isUnknownOrUndef())
1162 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001163
1164 // Handle LazyCompoundVals for the immediate super region. Other cases
1165 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001166 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001167 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001169 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1170 return RetrieveElement(LCV->getState(), R);
1171 }
Mike Stump1eb44332009-09-09 15:08:12 +00001172
Ted Kremeneka6275a52009-07-15 02:31:43 +00001173 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001174 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001175 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001176
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001177 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001178}
1179
Mike Stump1eb44332009-09-09 15:08:12 +00001180SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001181 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001182
1183 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001184 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001185 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001186 return *V;
1187
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001188 QualType Ty = R->getValueType(getContext());
1189 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1190}
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001192SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1193 const TypedRegion *R,
1194 QualType Ty,
1195 const MemRegion *superR) {
1196
Mike Stump1eb44332009-09-09 15:08:12 +00001197 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001198 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001200 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001202 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001203 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001204 if (SymbolRef parentSym = D->getAsSymbol())
1205 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001207 if (D->isZeroConstant())
1208 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001210 if (D->isUnknown())
1211 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001213 assert(0 && "Unknown default value");
1214 }
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001216 // If our super region is a field or element itself, walk up the region
1217 // hierarchy to see if there is a default value installed in an ancestor.
1218 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1219 superR = cast<SubRegion>(superR)->getSuperRegion();
1220 continue;
1221 }
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001223 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001224 }
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001226 // Lazy binding?
1227 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001228 const MemRegion *lazyBindingRegion = NULL;
1229 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001231 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001232 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001234 if (isa<ElementRegion>(R))
1235 return RetrieveElement(lazyBindingState,
1236 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001238 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001239 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001240 }
1241
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001242 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001244 if (isa<ElementRegion>(R)) {
1245 // Currently we don't reason specially about Clang-style vectors. Check
1246 // if superR is a vector and if so return Unknown.
1247 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1248 if (typedSuperR->getValueType(getContext())->isVectorType())
1249 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001250 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001251 }
Mike Stump1eb44332009-09-09 15:08:12 +00001252
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001253 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001254 }
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001256 // All other values are symbolic.
1257 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001258}
Mike Stump1eb44332009-09-09 15:08:12 +00001259
1260SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001261 const ObjCIvarRegion* R) {
1262
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001263 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001264 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001265
Zhongxing Xu13d50172009-10-11 08:08:02 +00001266 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001267 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001269 const MemRegion *superR = R->getSuperRegion();
1270
Ted Kremenekab22ee92009-10-20 01:20:57 +00001271 // Check if the super region has a default binding.
1272 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001273 if (SymbolRef parentSym = V->getAsSymbol())
1274 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276 // Other cases: give up.
1277 return UnknownVal();
1278 }
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Ted Kremenek25c54572009-07-20 22:58:02 +00001280 return RetrieveLazySymbol(state, R);
1281}
1282
Ted Kremenek9031dd72009-07-21 00:12:07 +00001283SVal RegionStoreManager::RetrieveVar(const GRState *state,
1284 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Ted Kremenek9031dd72009-07-21 00:12:07 +00001286 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001287 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Zhongxing Xu13d50172009-10-11 08:08:02 +00001289 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001290 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Ted Kremenek9031dd72009-07-21 00:12:07 +00001292 // Lazily derive a value for the VarRegion.
1293 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Ted Kremenek9031dd72009-07-21 00:12:07 +00001295 if (R->hasGlobalsOrParametersStorage())
1296 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001297
Ted Kremenek9031dd72009-07-21 00:12:07 +00001298 return UndefinedVal();
1299}
1300
Mike Stump1eb44332009-09-09 15:08:12 +00001301SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001302 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Ted Kremenek25c54572009-07-20 22:58:02 +00001304 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001305
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001306 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001307 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001308}
1309
Mike Stump1eb44332009-09-09 15:08:12 +00001310SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1311 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001312 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001313 assert(T->isStructureType());
1314
Zhongxing Xub7507d12009-06-11 07:27:30 +00001315 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001316 RecordDecl* RD = RT->getDecl();
1317 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001318 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001319#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001320 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1321
Ted Kremenek67f28532009-06-17 22:02:04 +00001322 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1323 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001324 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001325
Douglas Gregore267ff32008-12-11 20:41:00 +00001326 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1327 FieldEnd = Fields.rend();
1328 Field != FieldEnd; ++Field) {
1329 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001330 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001331 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001332 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1333 }
1334
Zhongxing Xud91ee272009-06-23 09:02:15 +00001335 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001336#else
1337 return ValMgr.makeLazyCompoundVal(state, R);
1338#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001339}
1340
Ted Kremenek67f28532009-06-17 22:02:04 +00001341SVal RegionStoreManager::RetrieveArray(const GRState *state,
1342 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001343#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001344 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001345 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1346
1347 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001348 uint64_t size = CAT->getSize().getZExtValue();
1349 for (uint64_t i = 0; i < size; ++i) {
1350 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001351 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001352 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001353 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001354 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001355 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1356 }
1357
Zhongxing Xud91ee272009-06-23 09:02:15 +00001358 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001359#else
1360 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1361 return ValMgr.makeLazyCompoundVal(state, R);
1362#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001363}
1364
Ted Kremenek9af46f52009-06-16 22:36:44 +00001365//===----------------------------------------------------------------------===//
1366// Binding values to regions.
1367//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001368
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001369Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001370 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Ted Kremenek0964a062009-01-21 06:57:53 +00001372 if (isa<loc::MemRegionVal>(L))
1373 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001374
Ted Kremenek0964a062009-01-21 06:57:53 +00001375 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001376 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001377 return RBFactory.Remove(B, R).getRoot();
1378 }
Mike Stump1eb44332009-09-09 15:08:12 +00001379
Ted Kremenek0964a062009-01-21 06:57:53 +00001380 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001381}
1382
Ted Kremenek67f28532009-06-17 22:02:04 +00001383const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001384 if (isa<loc::ConcreteInt>(L))
1385 return state;
1386
Ted Kremenek9af46f52009-06-16 22:36:44 +00001387 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001388 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Ted Kremenek9af46f52009-06-16 22:36:44 +00001390 // Check if the region is a struct region.
1391 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1392 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001393 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001394
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001395 // Special case: the current region represents a cast and it and the super
1396 // region both have pointer types or intptr_t types. If so, perform the
1397 // bind to the super region.
1398 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001399 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001400 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1401 if (ER->getIndex().isZeroConstant()) {
1402 if (const TypedRegion *superR =
1403 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1404 ASTContext &Ctx = getContext();
1405 QualType superTy = superR->getValueType(Ctx);
1406 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
1408 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001409 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001410 SValuator::CastResult cr =
1411 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001412 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1413 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001414 // For now, just invalidate the fields of the struct/union/class.
1415 // FIXME: Precisely handle the fields of the record.
1416 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001417 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001418 }
1419 }
1420 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001421 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1422 // Binding directly to a symbolic region should be treated as binding
1423 // to element 0.
1424 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001425 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001426 R = GetElementZeroRegion(SR, T);
1427 }
Mike Stump1eb44332009-09-09 15:08:12 +00001428
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001429 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001430 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001431 return state->makeWithStore(
1432 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001433}
1434
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001435const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001436 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001437 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001438
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001439 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001440
Ted Kremenek0964a062009-01-21 06:57:53 +00001441 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001442 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001443 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001444 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001445
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001446 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001447}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001448
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001449// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001450const GRState *
1451RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001452 const CompoundLiteralExpr *CL,
1453 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001454 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001455 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1456 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001457}
1458
Ted Kremenek027e2662009-11-19 20:20:24 +00001459const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1460 const MemRegion *R,
1461 QualType T) {
1462 Store store = state->getStore();
1463 RegionBindings B = GetRegionBindings(store);
1464 SVal V;
1465
1466 if (Loc::IsLocType(T))
1467 V = ValMgr.makeNull();
1468 else if (T->isIntegerType())
1469 V = ValMgr.makeZeroVal(T);
1470 else if (T->isStructureType() || T->isArrayType()) {
1471 // Set the default value to a zero constant when it is a structure
1472 // or array. The type doesn't really matter.
1473 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1474 }
1475 else {
1476 return state;
1477 }
1478
1479 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1480 return state->makeWithStore(B.getRoot());
1481}
1482
Ted Kremenek67f28532009-06-17 22:02:04 +00001483const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001484 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001485 SVal Init) {
1486
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001487 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001488 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001489 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001490
Ted Kremenek46537392009-07-16 01:33:37 +00001491 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001492
1493 // Check if the init expr is a StringLiteral.
1494 if (isa<loc::MemRegionVal>(Init)) {
1495 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1496 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1497 const char* str = S->getStrData();
1498 unsigned len = S->getByteLength();
1499 unsigned j = 0;
1500
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001501 // Copy bytes from the string literal into the target array. Trailing bytes
1502 // in the array that are not covered by the string literal are initialized
1503 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001504 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001505 if (j >= len)
1506 break;
1507
Ted Kremenek46537392009-07-16 01:33:37 +00001508 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001509 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1510 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001511
Zhongxing Xud91ee272009-06-23 09:02:15 +00001512 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001513 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001514 }
1515
Ted Kremenek67f28532009-06-17 22:02:04 +00001516 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001517 }
1518
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001519 // Handle lazy compound values.
1520 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1521 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001522
1523 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001524
1525 if (Init.isUnknown())
1526 return setImplicitDefaultValue(state, R, ElementTy);
1527
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001528 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001529 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001530 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Ted Kremenek46537392009-07-16 01:33:37 +00001532 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001533 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001534 if (VI == VE)
1535 break;
1536
Ted Kremenek46537392009-07-16 01:33:37 +00001537 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001538 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001539
1540 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001541 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001542 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001543 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001544 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001545 }
1546
Ted Kremenek027e2662009-11-19 20:20:24 +00001547 // If the init list is shorter than the array length, set the
1548 // array default value.
1549 if (i < size)
1550 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001551
Ted Kremenek67f28532009-06-17 22:02:04 +00001552 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001553}
1554
Ted Kremenek67f28532009-06-17 22:02:04 +00001555const GRState *
1556RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1557 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Ted Kremenek67f28532009-06-17 22:02:04 +00001559 if (!Features.supportsFields())
1560 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001562 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001563 assert(T->isStructureType());
1564
Ted Kremenek6217b802009-07-29 21:53:49 +00001565 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001566 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001567
1568 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001569 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001570
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001571 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001572 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001573 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001574
Ted Kremenek67f28532009-06-17 22:02:04 +00001575 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1576 // Ignore them and kill the field values.
1577 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001578 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001579
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001580 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001581 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001582
1583 RecordDecl::field_iterator FI, FE;
1584
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001585 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001586
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001587 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001588 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001589
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001590 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001591 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001592
Ted Kremenekcf549592009-09-22 21:19:14 +00001593 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001594 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001595 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001596 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001597 else
1598 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001599 }
1600
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001601 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001602 if (FI != FE) {
1603 Store store = state->getStore();
1604 RegionBindings B = GetRegionBindings(store);
1605 B = RBFactory.Add(B, R,
1606 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1607 state = state->makeWithStore(B.getRoot());
1608 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001609
Ted Kremenek67f28532009-06-17 22:02:04 +00001610 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001611}
1612
Zhongxing Xu13d50172009-10-11 08:08:02 +00001613Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1614 RegionBindings B = GetRegionBindings(store);
1615 llvm::OwningPtr<RegionStoreSubRegionMap>
1616 SubRegions(getRegionStoreSubRegionMap(store));
1617 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001618
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001619 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001620 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001621
Zhongxing Xu13d50172009-10-11 08:08:02 +00001622 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001623}
1624
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001625const GRState*
1626RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1627 const GRState *state,
1628 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001629
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001630 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001631 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001632
Mike Stump1eb44332009-09-09 15:08:12 +00001633 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001634 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001635
Mike Stump1eb44332009-09-09 15:08:12 +00001636 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001637 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001639 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1640 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001641 return state->makeWithStore(
1642 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001643}
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Ted Kremenek9af46f52009-06-16 22:36:44 +00001645//===----------------------------------------------------------------------===//
1646// State pruning.
1647//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001648
Mike Stump1eb44332009-09-09 15:08:12 +00001649void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001650 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001651 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001652{
Ted Kremenek781115c2009-10-17 17:45:11 +00001653 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1654
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001655 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001656 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Ted Kremenek9af46f52009-06-16 22:36:44 +00001658 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001659 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001660 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001661
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001662 // Do a pass over the regions in the store. For VarRegions we check if
1663 // the variable is still live and if so add it to the list of live roots.
1664 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001665 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001666
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001667 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001668 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001669 const MemRegion *R = I.getKey();
1670 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001671 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001672
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001673 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001674 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001675 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001676 llvm::SmallVector<RBDNode, 10> Postponed;
1677
Zhongxing Xu6800b332009-10-18 04:15:47 +00001678 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001679
Ted Kremenek9af46f52009-06-16 22:36:44 +00001680 while (!IntermediateRoots.empty()) {
1681 const MemRegion* R = IntermediateRoots.back();
1682 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001683
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001684 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001685 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001686 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001687
Ted Kremenek9af46f52009-06-16 22:36:44 +00001688 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001689 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001690 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001691 continue;
1692 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001693
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001694 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001695 llvm::SmallVectorImpl<RBDNode> &Q =
1696 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1697
1698 Q.push_back(std::make_pair(&state, SR));
1699
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001700 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001701 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001702
1703 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001704 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001705 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001706 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001709 // Enqueue the RegionRoots onto WorkList.
1710 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1711 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001712 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001713 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001714 RegionRoots.clear();
1715
Zhongxing Xu6800b332009-10-18 04:15:47 +00001716 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001717
Ted Kremenek01756192009-10-29 05:14:17 +00001718tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001719 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001720 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001721 WorkList.pop_back();
1722
1723 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001724 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001725 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001726 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001728 const MemRegion *R = N.second;
1729 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001730
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001731 // Enqueue subregions.
1732 RegionStoreSubRegionMap *M;
1733
1734 if (&state == state_N)
1735 M = SubRegions.get();
1736 else {
1737 RegionStoreSubRegionMap *& SM = SC[state_N];
1738 if (!SM)
1739 SM = getRegionStoreSubRegionMap(state_N->getStore());
1740 M = SM;
1741 }
1742
1743 RegionStoreSubRegionMap::iterator I, E;
1744 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1745 WorkList.push_back(std::make_pair(state_N, *I));
1746
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001747 // Enqueue the super region.
1748 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1749 const MemRegion *superR = SR->getSuperRegion();
1750 if (!isa<MemSpaceRegion>(superR)) {
1751 // If 'R' is a field or an element, we want to keep the bindings
1752 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001753 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001754 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1755 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001756 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001757 }
1758 }
1759
1760 // Mark the symbol for any live SymbolicRegion as "live". This means we
1761 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001762 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001763 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001764
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001765 // For BlockDataRegions, enqueue the VarRegions for variables marked
1766 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001767 // 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();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001771 RI != RE; ++RI) {
1772 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1773 WorkList.push_back(std::make_pair(state_N, *RI));
1774 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001775 // No possible data bindings on a BlockDataRegion. Continue to the
1776 // next region in the worklist.
1777 continue;
1778 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001779
1780 Store store_N = state_N->getStore();
1781 RegionBindings B_N = GetRegionBindings(store_N);
1782
1783 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001784 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001785
Zhongxing Xu13d50172009-10-11 08:08:02 +00001786 if (V) {
1787 // Check for lazy bindings.
1788 if (const nonloc::LazyCompoundVal *LCV =
1789 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001790
Zhongxing Xu13d50172009-10-11 08:08:02 +00001791 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001792 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001793 }
1794 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001795 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001796 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001797 SI!=SE;++SI)
1798 SymReaper.markLive(*SI);
1799
Zhongxing Xu13d50172009-10-11 08:08:02 +00001800 // If V is a region, then add it to the worklist.
1801 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001802 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001803 }
1804 }
1805 }
1806
Ted Kremenek01756192009-10-29 05:14:17 +00001807 // See if any postponed SymbolicRegions are actually live now, after
1808 // having done a scan.
1809 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1810 E = Postponed.end() ; I != E ; ++I) {
1811 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1812 if (SymReaper.isLive(SR->getSymbol())) {
1813 WorkList.push_back(*I);
1814 I->second = NULL;
1815 }
1816 }
1817 }
1818
1819 if (!WorkList.empty())
1820 goto tryAgain;
1821
Ted Kremenek9af46f52009-06-16 22:36:44 +00001822 // We have now scanned the store, marking reachable regions and symbols
1823 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001824 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001825 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001826 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001827 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001828 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001829 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Ted Kremenek9af46f52009-06-16 22:36:44 +00001831 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001832 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Ted Kremenek9af46f52009-06-16 22:36:44 +00001834 // Mark all non-live symbols that this region references as dead.
1835 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1836 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Zhongxing Xu13d50172009-10-11 08:08:02 +00001838 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001839 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1840 for (; SI != SE; ++SI)
1841 SymReaper.maybeDead(*SI);
1842 }
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001844 // Write the store back.
1845 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001846}
1847
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001848GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1849 StackFrameContext const *frame) {
1850 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1851 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1852
1853 FunctionDecl::param_const_iterator PI = FD->param_begin();
1854
1855 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1856
1857 // Copy the arg expression value to the arg variables.
1858 for (; AI != AE; ++AI, ++PI) {
1859 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001860 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001861 }
1862
1863 return state;
1864}
1865
Ted Kremenek9af46f52009-06-16 22:36:44 +00001866//===----------------------------------------------------------------------===//
1867// Utility methods.
1868//===----------------------------------------------------------------------===//
1869
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001870void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001871 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001872 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001873 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Ted Kremenek451ac092009-08-06 04:50:20 +00001875 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001876 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001877}