blob: ba63308c5da77a38c78321e5810b28ad1bba72ae [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 Kremeneka5e81f12009-08-06 01:20:57 +000031#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000032
Zhongxing Xu13d50172009-10-11 08:08:02 +000033namespace {
34class BindingVal {
35public:
36 enum BindingKind { Direct, Default };
37private:
38 SVal Value;
39 BindingKind Kind;
40
41public:
42 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
43
44 bool isDefault() const { return Kind == Default; }
45
46 const SVal *getValue() const { return &Value; }
47
48 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
49
50 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
51
52 void Profile(llvm::FoldingSetNodeID& ID) const {
53 Value.Profile(ID);
54 ID.AddInteger(Kind);
55 }
56
57 inline bool operator==(const BindingVal& R) const {
58 return Value == R.Value && Kind == R.Kind;
59 }
60
61 inline bool operator!=(const BindingVal& R) const {
62 return !(*this == R);
63 }
64};
65}
66
67namespace llvm {
68static inline
69llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
70 if (V.isDefault())
71 os << "(default) ";
72 else
73 os << "(direct) ";
74 os << *V.getValue();
75 return os;
76}
77} // end llvm namespace
78
Zhongxing Xubaf03a72008-11-24 09:44:56 +000079// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000080typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000081
Ted Kremenek50dc1b32008-12-24 01:05:03 +000082//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000083// Fine-grained control of RegionStoreManager.
84//===----------------------------------------------------------------------===//
85
86namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000087struct minimal_features_tag {};
88struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000089
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000090class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +000091 bool SupportsFields;
92 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenek9af46f52009-06-16 22:36:44 +000094public:
95 RegionStoreFeatures(minimal_features_tag) :
96 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000097
Ted Kremenek9af46f52009-06-16 22:36:44 +000098 RegionStoreFeatures(maximal_features_tag) :
99 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Ted Kremenek9af46f52009-06-16 22:36:44 +0000101 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek9af46f52009-06-16 22:36:44 +0000103 bool supportsFields() const { return SupportsFields; }
104 bool supportsRemaining() const { return SupportsRemaining; }
105};
106}
107
108//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000109// Region "Extents"
110//===----------------------------------------------------------------------===//
111//
112// MemRegions represent chunks of memory with a size (their "extent"). This
113// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000114//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000115namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000116static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000117namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000118 template<> struct GRStateTrait<RegionExtents>
119 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
120 static void* GDMIndex() { return &RegionExtentsIndex; }
121 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000122}
123
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000124//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000125// Utility functions.
126//===----------------------------------------------------------------------===//
127
128static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
129 if (ty->isAnyPointerType())
130 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000132 return ty->isIntegerType() && ty->isScalarType() &&
133 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
134}
135
136//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000137// Main RegionStore logic.
138//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000139
Zhongxing Xu17892752008-10-08 02:50:44 +0000140namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000142class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000143 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000144 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000145 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000146 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000148 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000149 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000150
151 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000152 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000153 return true;
154 }
155
156 I->second = F.Add(I->second, SubRegion);
157 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000158 }
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000160 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremenek59e8f112009-03-03 01:35:36 +0000162 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Ted Kremenek5dc27462009-03-03 02:51:43 +0000164 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000165 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000166
167 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000168 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Ted Kremenek59e8f112009-03-03 01:35:36 +0000170 llvm::ImmutableSet<const MemRegion*> S = I->second;
171 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
172 SI != SE; ++SI) {
173 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000174 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek5dc27462009-03-03 02:51:43 +0000177 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000180 typedef SetTy::iterator iterator;
181
182 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
183 Map::iterator I = M.find(R);
184 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
185 return std::make_pair(S.begin(), S.end());
186 }
Mike Stump1eb44332009-09-09 15:08:12 +0000187};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000188
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000189class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000190 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000191 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000192
193 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
194 SMCache SC;
195
Zhongxing Xu17892752008-10-08 02:50:44 +0000196public:
Mike Stump1eb44332009-09-09 15:08:12 +0000197 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000198 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000199 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000200 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000201
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000202 virtual ~RegionStoreManager() {
203 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
204 delete (*I).second;
205 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000206
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000207 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Zhongxing Xu13d50172009-10-11 08:08:02 +0000209 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Zhongxing Xu13d50172009-10-11 08:08:02 +0000211 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
212 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000213 /// getDefaultBinding - Returns an SVal* representing an optional default
214 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000215 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000216
217 /// setImplicitDefaultValue - Set the default binding for the provided
218 /// MemRegion to the value implicitly defined for compound literals when
219 /// the value is not specified.
220 const GRState *setImplicitDefaultValue(const GRState *state,
221 const MemRegion *R,
222 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000224 /// getLValueString - Returns an SVal representing the lvalue of a
225 /// StringLiteral. Within RegionStore a StringLiteral has an
226 /// associated StringRegion, and the lvalue of a StringLiteral is
227 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000228 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000229
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000230 /// getLValueCompoundLiteral - Returns an SVal representing the
231 /// lvalue of a compound literal. Within RegionStore a compound
232 /// literal has an associated region, and the lvalue of the
233 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000234 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000235
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000236 /// getLValueVar - Returns an SVal that represents the lvalue of a
237 /// variable. Within RegionStore a variable has an associated
238 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000239 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000241 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000242
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000243 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000245 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000246
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000247 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000248
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000249
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000250 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
251 /// type. 'Array' represents the lvalue of the array being decayed
252 /// to a pointer, and the returned SVal represents the decayed
253 /// version of that lvalue (i.e., a pointer to the first element of
254 /// the array). This is called by GRExprEngine when evaluating
255 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000256 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000257
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000258 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000259 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000260
Mike Stump1eb44332009-09-09 15:08:12 +0000261 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000262 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000263 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000264
Ted Kremenek67f28532009-06-17 22:02:04 +0000265 //===-------------------------------------------------------------------===//
266 // Binding values to regions.
267 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000268
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000269 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000270 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-12-03 03:27:11 +0000271 InvalidatedSymbols *IS) {
272 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
273 }
274
275 const GRState *InvalidateRegions(const GRState *state,
276 const MemRegion * const *Begin,
277 const MemRegion * const *End,
278 const Expr *E, unsigned Count,
279 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000280
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000281private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000282 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000283 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000284
285public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000286 const GRState *Bind(const GRState *state, Loc LV, SVal V);
287
288 const GRState *BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +0000289 const CompoundLiteralExpr* CL,
290 const LocationContext *LC,
291 SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000293 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
294 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000295
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000296 const GRState *BindDeclWithNoInit(const GRState *state,
297 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000298 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000299 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000300
Ted Kremenek67f28532009-06-17 22:02:04 +0000301 /// BindStruct - Bind a compound value to a structure.
302 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Ted Kremenek67f28532009-06-17 22:02:04 +0000304 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000305
306 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000307 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000308
Ted Kremenek67f28532009-06-17 22:02:04 +0000309 Store Remove(Store store, Loc LV);
310
311 //===------------------------------------------------------------------===//
312 // Loading values from regions.
313 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Ted Kremenek67f28532009-06-17 22:02:04 +0000315 /// The high level logic for this method is this:
316 /// Retrieve (L)
317 /// if L has binding
318 /// return L's binding
319 /// else if L is in killset
320 /// return unknown
321 /// else
322 /// if L is on stack or heap
323 /// return undefined
324 /// else
325 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000326 SValuator::CastResult Retrieve(const GRState *state, Loc L,
327 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000328
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000329 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000330
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000331 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000333 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremenek9031dd72009-07-21 00:12:07 +0000335 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Ted Kremenek25c54572009-07-20 22:58:02 +0000337 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000339 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
340 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Ted Kremenek67f28532009-06-17 22:02:04 +0000342 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000343 /// struct copy:
344 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000345 /// x = y;
346 /// y's value is retrieved by this method.
347 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Ted Kremenek67f28532009-06-17 22:02:04 +0000349 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Zhongxing Xu944ebc62009-12-21 06:52:24 +0000351 /// Get the state and region whose binding this region R corresponds to.
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 Kremenekc410d4d2009-12-16 23:53:37 +0000542 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000543 // Invalidate the region by setting its default value to
544 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000545 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
546 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000547 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000548 continue;
549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Ted Kremenek87806792009-09-27 20:45:21 +0000551 if (!R->isBoundable())
552 continue;
553
554 const TypedRegion *TR = cast<TypedRegion>(R);
555 QualType T = TR->getValueType(Ctx);
556
557 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000558 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
559
Zhongxing Xu13d50172009-10-11 08:08:02 +0000560 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000561 if (!RD)
562 continue;
563
Zhongxing Xu13d50172009-10-11 08:08:02 +0000564 // Invalidate the region by setting its default value to
565 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000566 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
567 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000568 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000569 continue;
570 }
571
572 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
573 // Set the default value of the array to conjured symbol.
574 DefinedOrUnknownSVal V =
575 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000576 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000577 continue;
578 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000579
580 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
581 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000582 // For fields and elements whose super region has also been invalidated,
583 // only remove the old binding. The super region will get set with a
584 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000585 B = RBFactory.Remove(B, R);
586 continue;
587 }
Ted Kremenek87806792009-09-27 20:45:21 +0000588
Ted Kremenek389c44c2009-09-29 03:12:50 +0000589 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000590 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
591 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000592 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000593 }
594
Ted Kremenek87806792009-09-27 20:45:21 +0000595 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000596 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000597}
598
599//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000600// getLValueXXX methods.
601//===----------------------------------------------------------------------===//
602
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000603/// getLValueString - Returns an SVal representing the lvalue of a
604/// StringLiteral. Within RegionStore a StringLiteral has an
605/// associated StringRegion, and the lvalue of a StringLiteral is the
606/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000607SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000608 return loc::MemRegionVal(MRMgr.getStringRegion(S));
609}
610
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000611/// getLValueVar - Returns an SVal that represents the lvalue of a
612/// variable. Within RegionStore a variable has an associated
613/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000614SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000615 const LocationContext *LC) {
616 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000617}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000618
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000619SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
620 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000621}
622
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000623SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
624 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000625}
626
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000627SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000628 if (Base.isUnknownOrUndef())
629 return Base;
630
631 Loc BaseL = cast<Loc>(Base);
632 const MemRegion* BaseR = 0;
633
634 switch (BaseL.getSubKind()) {
635 case loc::MemRegionKind:
636 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
637 break;
638
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000639 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000640 // These are anormal cases. Flag an undefined value.
641 return UndefinedVal();
642
643 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000644 // While these seem funny, this can happen through casts.
645 // FIXME: What we should return is the field offset. For example,
646 // add the field offset to the integer value. That way funny things
647 // like this work properly: &(((struct foo *) 0xa)->f)
648 return Base;
649
650 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000651 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000652 return Base;
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000655 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
656 // of FieldDecl.
657 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
658 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000659
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000660 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000661}
662
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000663SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
664 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000665
Ted Kremenekde7ec632009-03-09 22:44:49 +0000666 // If the base is an unknown or undefined value, just return it back.
667 // FIXME: For absolute pointer addresses, we just return that value back as
668 // well, although in reality we should return the offset added to that
669 // value.
670 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000671 return Base;
672
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000673 // Only handle integer offsets... for now.
674 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000675 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000676
Zhongxing Xuce760782009-05-09 13:20:07 +0000677 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000678
679 // Pointer of any type can be cast and used as array base.
680 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Ted Kremenek46537392009-07-16 01:33:37 +0000682 // Convert the offset to the appropriate size and signedness.
683 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000685 if (!ElemR) {
686 //
687 // If the base region is not an ElementRegion, create one.
688 // This can happen in the following example:
689 //
690 // char *p = __builtin_alloc(10);
691 // p[1] = 8;
692 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000693 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000694 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000695 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000696 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000697 }
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000699 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000701 if (!isa<nonloc::ConcreteInt>(BaseIdx))
702 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000704 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
705 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
706 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Ted Kremenek46537392009-07-16 01:33:37 +0000708 // Compute the new index.
709 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Ted Kremenek46537392009-07-16 01:33:37 +0000711 // Construct the new ElementRegion.
712 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000713 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000714 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000715}
716
Ted Kremenek9af46f52009-06-16 22:36:44 +0000717//===----------------------------------------------------------------------===//
718// Extents for regions.
719//===----------------------------------------------------------------------===//
720
Zhongxing Xue884ff82009-11-12 02:48:32 +0000721DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
722 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000724 switch (R->getKind()) {
Ted Kremenekde0d2632010-01-05 02:18:06 +0000725 case MemRegion::CXXThisRegionKind:
726 assert(0 && "Cannot get size of 'this' region");
Ted Kremenek67d12872009-12-07 22:05:27 +0000727 case MemRegion::GenericMemSpaceRegionKind:
728 case MemRegion::StackLocalsSpaceRegionKind:
729 case MemRegion::StackArgumentsSpaceRegionKind:
730 case MemRegion::HeapSpaceRegionKind:
731 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000732 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000733 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000734 return UnknownVal();
735
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000736 case MemRegion::FunctionTextRegionKind:
737 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000738 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000739 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000740 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000741
742 // Not yet handled.
743 case MemRegion::AllocaRegionKind:
744 case MemRegion::CompoundLiteralRegionKind:
745 case MemRegion::ElementRegionKind:
746 case MemRegion::FieldRegionKind:
747 case MemRegion::ObjCIvarRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000748 case MemRegion::SymbolicRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000749 case MemRegion::CXXObjectRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000750 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000752 case MemRegion::StringRegionKind: {
753 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000754 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000755 // operations with signed indices.
756 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000759 case MemRegion::VarRegionKind: {
760 const VarRegion* VR = cast<VarRegion>(R);
761 // Get the type of the variable.
762 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000764 // FIXME: Handle variable-length arrays.
765 if (isa<VariableArrayType>(T))
766 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000768 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
769 // return the size as signed integer.
770 return ValMgr.makeIntVal(CAT->getSize(), false);
771 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000772
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000773 // Clients can use ordinary variables as if they were arrays. These
774 // essentially are arrays of size 1.
775 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000776 }
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000779 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000780 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000781}
782
Ted Kremenek67f28532009-06-17 22:02:04 +0000783const GRState *RegionStoreManager::setExtent(const GRState *state,
784 const MemRegion *region,
785 SVal extent) {
786 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000787}
788
789//===----------------------------------------------------------------------===//
790// Location and region casting.
791//===----------------------------------------------------------------------===//
792
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000793/// ArrayToPointer - Emulates the "decay" of an array to a pointer
794/// type. 'Array' represents the lvalue of the array being decayed
795/// to a pointer, and the returned SVal represents the decayed
796/// version of that lvalue (i.e., a pointer to the first element of
797/// the array). This is called by GRExprEngine when evaluating casts
798/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000799SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000800 if (!isa<loc::MemRegionVal>(Array))
801 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Ted Kremenekabb042f2008-12-13 19:24:37 +0000803 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
804 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000806 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000807 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000808
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000809 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000810 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000811 ArrayType *AT = cast<ArrayType>(T);
812 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenek75185b52009-07-16 00:00:11 +0000814 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000815 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
816 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000817}
818
Ted Kremenek9af46f52009-06-16 22:36:44 +0000819//===----------------------------------------------------------------------===//
820// Pointer arithmetic.
821//===----------------------------------------------------------------------===//
822
Mike Stump1eb44332009-09-09 15:08:12 +0000823SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000824 BinaryOperator::Opcode Op, Loc L, NonLoc R,
825 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000826 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000827 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000828 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000829
Zhongxing Xua1718c72009-04-03 07:33:13 +0000830 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000831 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000832
Ted Kremenek3bccf082009-07-11 00:58:27 +0000833 switch (MR->getKind()) {
834 case MemRegion::SymbolicRegionKind: {
835 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000836 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000837 QualType T = Sym->getType(getContext());
838 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000840 if (const PointerType *PT = T->getAs<PointerType>())
841 EleTy = PT->getPointeeType();
842 else
John McCall183700f2009-09-21 23:43:11 +0000843 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Ted Kremenek3bccf082009-07-11 00:58:27 +0000845 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
846 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000847 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000848 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000849 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000850 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000851 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000852 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000853 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
854 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000855 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000856 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000857
Ted Kremenek3bccf082009-07-11 00:58:27 +0000858 case MemRegion::ElementRegionKind: {
859 ER = cast<ElementRegion>(MR);
860 break;
861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Ted Kremenek3bccf082009-07-11 00:58:27 +0000863 // Not yet handled.
864 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000865 case MemRegion::StringRegionKind: {
866
867 }
868 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000869 case MemRegion::CompoundLiteralRegionKind:
870 case MemRegion::FieldRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000871 case MemRegion::ObjCIvarRegionKind:
Zhongxing Xubb141212009-12-16 11:27:52 +0000872 case MemRegion::CXXObjectRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000873 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000875 case MemRegion::FunctionTextRegionKind:
876 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000877 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000878 // Technically this can happen if people do funny things with casts.
879 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Ted Kremenekde0d2632010-01-05 02:18:06 +0000881 case MemRegion::CXXThisRegionKind:
882 assert(0 &&
883 "Cannot perform pointer arithmetic on implicit argument 'this'");
Ted Kremenek67d12872009-12-07 22:05:27 +0000884 case MemRegion::GenericMemSpaceRegionKind:
885 case MemRegion::StackLocalsSpaceRegionKind:
886 case MemRegion::StackArgumentsSpaceRegionKind:
887 case MemRegion::HeapSpaceRegionKind:
888 case MemRegion::GlobalsSpaceRegionKind:
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000889 case MemRegion::UnknownSpaceRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000890 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
891 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000892 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000893
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000894 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000895 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000896
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000897 // For now, only support:
898 // (a) concrete integer indices that can easily be resolved
899 // (b) 0 + symbolic index
900 if (Base) {
901 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
902 // FIXME: Should use SValuator here.
903 SVal NewIdx =
904 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000905 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000906 const MemRegion* NewER =
907 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
908 ER->getSuperRegion(), getContext());
909 return ValMgr.makeLoc(NewER);
910 }
911 if (0 == Base->getValue()) {
912 const MemRegion* NewER =
913 MRMgr.getElementRegion(ER->getElementType(), R,
914 ER->getSuperRegion(), getContext());
915 return ValMgr.makeLoc(NewER);
916 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000917 }
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Ted Kremenek5dc27462009-03-03 02:51:43 +0000919 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000920}
921
Ted Kremenek9af46f52009-06-16 22:36:44 +0000922//===----------------------------------------------------------------------===//
923// Loading values from regions.
924//===----------------------------------------------------------------------===//
925
Zhongxing Xu13d50172009-10-11 08:08:02 +0000926Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
927 const MemRegion *R) {
928 if (const BindingVal *BV = B.lookup(R))
929 return Optional<SVal>::create(BV->getDirectValue());
930
931 return Optional<SVal>();
932}
933
934Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000935 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000937 if (R->isBoundable())
938 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
939 if (TR->getValueType(getContext())->isUnionType())
940 return UnknownVal();
941
Zhongxing Xu13d50172009-10-11 08:08:02 +0000942 if (BindingVal const *V = B.lookup(R))
943 return Optional<SVal>::create(V->getDefaultValue());
944
945 return Optional<SVal>();
946}
947
948Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
949 const MemRegion *R) {
950 if (const BindingVal *BV = B.lookup(R))
951 return Optional<SVal>::create(BV->getValue());
952
953 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000954}
955
Ted Kremeneka6275a52009-07-15 02:31:43 +0000956static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
957 RTy = Ctx.getCanonicalType(RTy);
958 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Ted Kremeneka6275a52009-07-15 02:31:43 +0000960 if (RTy == UsedTy)
961 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000962
963
Ted Kremenek25c54572009-07-20 22:58:02 +0000964 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000965 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000966 // represents a reinterpretation.
967 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000968 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000969 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000970
971 return PUsedTy && PRTy &&
972 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000973 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000974 }
975
976 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000977}
978
Ted Kremenek0954cde2009-09-24 04:11:44 +0000979const ElementRegion *
980RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
981 ASTContext &Ctx = getContext();
982 SVal idx = ValMgr.makeZeroArrayIndex();
983 assert(!T.isNull());
984 return MRMgr.getElementRegion(T, idx, SR, Ctx);
985}
986
987
988
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000989SValuator::CastResult
990RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000991
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000992 assert(!isa<UnknownVal>(L) && "location unknown");
993 assert(!isa<UndefinedVal>(L) && "location undefined");
Ted Kremenek28172a22009-12-15 23:23:27 +0000994
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000995 // FIXME: Is this even possible? Shouldn't this be treated as a null
996 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000997 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000998 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek28172a22009-12-15 23:23:27 +0000999
Ted Kremenek67f28532009-06-17 22:02:04 +00001000 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001001
Zhongxing Xu91844122009-05-20 09:18:48 +00001002 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001003 // Example:
1004 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001005 // char* p = alloca();
1006 // read(p);
1007 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001008 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001009 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Ted Kremenek0954cde2009-09-24 04:11:44 +00001011 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1012 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Ted Kremenek968f0a62009-08-03 21:41:46 +00001014 if (isa<CodeTextRegion>(MR))
1015 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001017 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1018 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001019 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001020 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001021
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001022 // FIXME: We should eventually handle funny addressing. e.g.:
1023 //
1024 // int x = ...;
1025 // int *p = &x;
1026 // char *q = (char*) p;
1027 // char c = *q; // returns the first byte of 'x'.
1028 //
1029 // Such funny addressing will occur due to layering of regions.
1030
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001031#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001032 ASTContext &Ctx = getContext();
1033 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001034 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1035 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001036 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001037 assert(Ctx.getCanonicalType(RTy) ==
1038 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001039 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001040#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001041
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001042 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001043 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001045 // FIXME: Handle unions.
1046 if (RTy->isUnionType())
1047 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001048
1049 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001050 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001051
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001052 // FIXME: handle Vector types.
1053 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001054 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001055
1056 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001057 return SValuator::CastResult(state,
1058 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001059
1060 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001061 return SValuator::CastResult(state,
1062 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek25c54572009-07-20 22:58:02 +00001064 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001065 return SValuator::CastResult(state,
1066 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Ted Kremenek9031dd72009-07-21 00:12:07 +00001068 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001069 return SValuator::CastResult(state,
1070 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001071
Ted Kremenek451ac092009-08-06 04:50:20 +00001072 RegionBindings B = GetRegionBindings(state->getStore());
1073 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001074
1075 // Check if the region has a binding.
1076 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001077 if (SVal const *SV = V->getValue())
1078 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001079
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001080 // The location does not have a bound value. This means that it has
1081 // the value it had upon its creation and/or entry to the analyzed
1082 // function/method. These are either symbolic values or 'undefined'.
Ted Kremenekde0d2632010-01-05 02:18:06 +00001083 if (R->hasStackNonParametersStorage()) {
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001084 // All stack variables are considered to have undefined values
1085 // upon creation. All heap allocated blocks are considered to
1086 // have undefined values as well unless they are explicitly bound
1087 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001088 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001089 }
1090
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001091 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001092 return SValuator::CastResult(state, ValMgr.getRegionValueSymbolVal(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 Kremenekde0d2632010-01-05 02:18:06 +00001242 if (R->hasStackNonParametersStorage()) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001243 if (isa<ElementRegion>(R)) {
1244 // Currently we don't reason specially about Clang-style vectors. Check
1245 // if superR is a vector and if so return Unknown.
1246 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1247 if (typedSuperR->getValueType(getContext())->isVectorType())
1248 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001249 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001250 }
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001252 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001253 }
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001255 // All other values are symbolic.
Ted Kremenek28172a22009-12-15 23:23:27 +00001256 return ValMgr.getRegionValueSymbolVal(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001257}
Mike Stump1eb44332009-09-09 15:08:12 +00001258
1259SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001260 const ObjCIvarRegion* R) {
1261
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001262 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001263 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001264
Zhongxing Xu13d50172009-10-11 08:08:02 +00001265 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001266 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001268 const MemRegion *superR = R->getSuperRegion();
1269
Ted Kremenekab22ee92009-10-20 01:20:57 +00001270 // Check if the super region has a default binding.
1271 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001272 if (SymbolRef parentSym = V->getAsSymbol())
1273 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001274
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001275 // Other cases: give up.
1276 return UnknownVal();
1277 }
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Ted Kremenek25c54572009-07-20 22:58:02 +00001279 return RetrieveLazySymbol(state, R);
1280}
1281
Ted Kremenek9031dd72009-07-21 00:12:07 +00001282SVal RegionStoreManager::RetrieveVar(const GRState *state,
1283 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Ted Kremenek9031dd72009-07-21 00:12:07 +00001285 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001286 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Zhongxing Xu13d50172009-10-11 08:08:02 +00001288 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001289 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Ted Kremenek9031dd72009-07-21 00:12:07 +00001291 // Lazily derive a value for the VarRegion.
1292 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Ted Kremenek2b87ae42009-12-11 06:43:27 +00001294 if (R->hasGlobalsOrParametersStorage() ||
1295 isa<UnknownSpaceRegion>(R->getMemorySpace()))
Ted Kremenek28172a22009-12-15 23:23:27 +00001296 return ValMgr.getRegionValueSymbolVal(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 Kremenek28172a22009-12-15 23:23:27 +00001307 return ValMgr.getRegionValueSymbolVal(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 Kremenek852274d2009-12-16 03:18:58 +00001425
1426 // FIXME: Is this the right way to handle symbols that are references?
1427 if (const PointerType *PT = T->getAs<PointerType>())
1428 T = PT->getPointeeType();
1429 else
1430 T = T->getAs<ReferenceType>()->getPointeeType();
1431
Ted Kremenek0954cde2009-09-24 04:11:44 +00001432 R = GetElementZeroRegion(SR, T);
1433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001435 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001436 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001437 return state->makeWithStore(
1438 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001439}
1440
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001441const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001442 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001443 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001444
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001445 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001446
Ted Kremenek0964a062009-01-21 06:57:53 +00001447 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001448 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001449 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001450 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001451
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001452 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001453}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001454
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001455// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001456const GRState *
1457RegionStoreManager::BindCompoundLiteral(const GRState *state,
Ted Kremenek67d12872009-12-07 22:05:27 +00001458 const CompoundLiteralExpr *CL,
1459 const LocationContext *LC,
Ted Kremenek67f28532009-06-17 22:02:04 +00001460 SVal V) {
Ted Kremenek67d12872009-12-07 22:05:27 +00001461 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC)),
1462 V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001463}
1464
Ted Kremenek027e2662009-11-19 20:20:24 +00001465const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1466 const MemRegion *R,
1467 QualType T) {
1468 Store store = state->getStore();
1469 RegionBindings B = GetRegionBindings(store);
1470 SVal V;
1471
1472 if (Loc::IsLocType(T))
1473 V = ValMgr.makeNull();
1474 else if (T->isIntegerType())
1475 V = ValMgr.makeZeroVal(T);
1476 else if (T->isStructureType() || T->isArrayType()) {
1477 // Set the default value to a zero constant when it is a structure
1478 // or array. The type doesn't really matter.
1479 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1480 }
1481 else {
1482 return state;
1483 }
1484
1485 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1486 return state->makeWithStore(B.getRoot());
1487}
1488
Ted Kremenek67f28532009-06-17 22:02:04 +00001489const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001490 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001491 SVal Init) {
1492
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001493 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001494 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001495 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001496
Ted Kremenek46537392009-07-16 01:33:37 +00001497 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001498
1499 // Check if the init expr is a StringLiteral.
1500 if (isa<loc::MemRegionVal>(Init)) {
1501 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1502 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1503 const char* str = S->getStrData();
1504 unsigned len = S->getByteLength();
1505 unsigned j = 0;
1506
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001507 // Copy bytes from the string literal into the target array. Trailing bytes
1508 // in the array that are not covered by the string literal are initialized
1509 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001510 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001511 if (j >= len)
1512 break;
1513
Ted Kremenek46537392009-07-16 01:33:37 +00001514 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001515 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1516 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001517
Zhongxing Xud91ee272009-06-23 09:02:15 +00001518 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001519 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001520 }
1521
Ted Kremenek67f28532009-06-17 22:02:04 +00001522 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001523 }
1524
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001525 // Handle lazy compound values.
1526 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1527 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001528
1529 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001530
1531 if (Init.isUnknown())
1532 return setImplicitDefaultValue(state, R, ElementTy);
1533
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001534 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001535 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001536 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Ted Kremenek46537392009-07-16 01:33:37 +00001538 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001539 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001540 if (VI == VE)
1541 break;
1542
Ted Kremenek46537392009-07-16 01:33:37 +00001543 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001544 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001545
1546 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001547 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001548 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001549 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001550 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001551 }
1552
Ted Kremenek027e2662009-11-19 20:20:24 +00001553 // If the init list is shorter than the array length, set the
1554 // array default value.
1555 if (i < size)
1556 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001557
Ted Kremenek67f28532009-06-17 22:02:04 +00001558 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001559}
1560
Ted Kremenek67f28532009-06-17 22:02:04 +00001561const GRState *
1562RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1563 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Ted Kremenek67f28532009-06-17 22:02:04 +00001565 if (!Features.supportsFields())
1566 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001568 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001569 assert(T->isStructureType());
1570
Ted Kremenek6217b802009-07-29 21:53:49 +00001571 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001572 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001573
1574 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001575 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001576
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001577 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001578 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001579 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Ted Kremenek67f28532009-06-17 22:02:04 +00001581 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1582 // Ignore them and kill the field values.
1583 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001584 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001585
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001586 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001587 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001588
1589 RecordDecl::field_iterator FI, FE;
1590
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001591 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001592
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001593 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001594 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001595
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001596 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001597 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001598
Ted Kremenekcf549592009-09-22 21:19:14 +00001599 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001600 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001601 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001602 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001603 else
1604 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001605 }
1606
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001607 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001608 if (FI != FE) {
1609 Store store = state->getStore();
1610 RegionBindings B = GetRegionBindings(store);
1611 B = RBFactory.Add(B, R,
1612 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1613 state = state->makeWithStore(B.getRoot());
1614 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001615
Ted Kremenek67f28532009-06-17 22:02:04 +00001616 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001617}
1618
Zhongxing Xu13d50172009-10-11 08:08:02 +00001619Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1620 RegionBindings B = GetRegionBindings(store);
1621 llvm::OwningPtr<RegionStoreSubRegionMap>
1622 SubRegions(getRegionStoreSubRegionMap(store));
1623 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001624
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001625 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001626 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001627
Zhongxing Xu13d50172009-10-11 08:08:02 +00001628 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001629}
1630
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001631const GRState*
1632RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1633 const GRState *state,
1634 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001635
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001636 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001637 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001638
Mike Stump1eb44332009-09-09 15:08:12 +00001639 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001640 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001641
Mike Stump1eb44332009-09-09 15:08:12 +00001642 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001643 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001645 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1646 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001647 return state->makeWithStore(
1648 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001649}
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Ted Kremenek9af46f52009-06-16 22:36:44 +00001651//===----------------------------------------------------------------------===//
1652// State pruning.
1653//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001654
Mike Stump1eb44332009-09-09 15:08:12 +00001655void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001656 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001657 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001658{
Ted Kremenek781115c2009-10-17 17:45:11 +00001659 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1660
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001661 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001662 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Ted Kremenek9af46f52009-06-16 22:36:44 +00001664 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001665 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001666 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001667
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001668 // Do a pass over the regions in the store. For VarRegions we check if
1669 // the variable is still live and if so add it to the list of live roots.
1670 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001671 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001672
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001673 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001674 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001675 const MemRegion *R = I.getKey();
1676 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001677 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001678
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001679 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001680 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001681 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001682 llvm::SmallVector<RBDNode, 10> Postponed;
1683
Zhongxing Xu6800b332009-10-18 04:15:47 +00001684 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001685
Ted Kremenek9af46f52009-06-16 22:36:44 +00001686 while (!IntermediateRoots.empty()) {
1687 const MemRegion* R = IntermediateRoots.back();
1688 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001689
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001690 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001691 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001692 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001693
Ted Kremenek9af46f52009-06-16 22:36:44 +00001694 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001695 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001696 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001697 continue;
1698 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001699
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001700 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001701 llvm::SmallVectorImpl<RBDNode> &Q =
1702 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1703
1704 Q.push_back(std::make_pair(&state, SR));
1705
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001706 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001707 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001708
1709 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001710 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001711 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001712 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001715 // Enqueue the RegionRoots onto WorkList.
1716 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1717 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001718 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001719 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001720 RegionRoots.clear();
1721
Zhongxing Xu6800b332009-10-18 04:15:47 +00001722 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001723
Ted Kremenek01756192009-10-29 05:14:17 +00001724tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001725 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001726 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001727 WorkList.pop_back();
1728
1729 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001730 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001731 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001732 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001733
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001734 const MemRegion *R = N.second;
1735 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001736
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001737 // Enqueue subregions.
1738 RegionStoreSubRegionMap *M;
1739
1740 if (&state == state_N)
1741 M = SubRegions.get();
1742 else {
1743 RegionStoreSubRegionMap *& SM = SC[state_N];
1744 if (!SM)
1745 SM = getRegionStoreSubRegionMap(state_N->getStore());
1746 M = SM;
1747 }
1748
1749 RegionStoreSubRegionMap::iterator I, E;
1750 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1751 WorkList.push_back(std::make_pair(state_N, *I));
1752
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001753 // Enqueue the super region.
1754 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1755 const MemRegion *superR = SR->getSuperRegion();
1756 if (!isa<MemSpaceRegion>(superR)) {
1757 // If 'R' is a field or an element, we want to keep the bindings
1758 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001759 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001760 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1761 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001762 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001763 }
1764 }
1765
1766 // Mark the symbol for any live SymbolicRegion as "live". This means we
1767 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001768 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001769 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001770
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001771 // For BlockDataRegions, enqueue the VarRegions for variables marked
1772 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001773 // via BlockDeclRefExprs.
1774 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1775 for (BlockDataRegion::referenced_vars_iterator
1776 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001777 RI != RE; ++RI) {
1778 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1779 WorkList.push_back(std::make_pair(state_N, *RI));
1780 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001781 // No possible data bindings on a BlockDataRegion. Continue to the
1782 // next region in the worklist.
1783 continue;
1784 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001785
1786 Store store_N = state_N->getStore();
1787 RegionBindings B_N = GetRegionBindings(store_N);
1788
1789 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001790 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001791
Zhongxing Xu13d50172009-10-11 08:08:02 +00001792 if (V) {
1793 // Check for lazy bindings.
1794 if (const nonloc::LazyCompoundVal *LCV =
1795 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001796
Zhongxing Xu13d50172009-10-11 08:08:02 +00001797 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001798 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001799 }
1800 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001801 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001802 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001803 SI!=SE;++SI)
1804 SymReaper.markLive(*SI);
1805
Zhongxing Xu13d50172009-10-11 08:08:02 +00001806 // If V is a region, then add it to the worklist.
1807 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001808 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001809 }
1810 }
1811 }
1812
Ted Kremenek01756192009-10-29 05:14:17 +00001813 // See if any postponed SymbolicRegions are actually live now, after
1814 // having done a scan.
1815 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1816 E = Postponed.end() ; I != E ; ++I) {
1817 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1818 if (SymReaper.isLive(SR->getSymbol())) {
1819 WorkList.push_back(*I);
1820 I->second = NULL;
1821 }
1822 }
1823 }
1824
1825 if (!WorkList.empty())
1826 goto tryAgain;
1827
Ted Kremenek9af46f52009-06-16 22:36:44 +00001828 // We have now scanned the store, marking reachable regions and symbols
1829 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001830 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001831 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001832 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001833 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001834 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001835 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Ted Kremenek9af46f52009-06-16 22:36:44 +00001837 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001838 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Ted Kremenek9af46f52009-06-16 22:36:44 +00001840 // Mark all non-live symbols that this region references as dead.
1841 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1842 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Zhongxing Xu13d50172009-10-11 08:08:02 +00001844 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001845 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1846 for (; SI != SE; ++SI)
1847 SymReaper.maybeDead(*SI);
1848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001850 // Write the store back.
1851 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001852}
1853
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001854GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1855 StackFrameContext const *frame) {
1856 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1857 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1858
1859 FunctionDecl::param_const_iterator PI = FD->param_begin();
1860
1861 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1862
1863 // Copy the arg expression value to the arg variables.
1864 for (; AI != AE; ++AI, ++PI) {
1865 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001866 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001867 }
1868
1869 return state;
1870}
1871
Ted Kremenek9af46f52009-06-16 22:36:44 +00001872//===----------------------------------------------------------------------===//
1873// Utility methods.
1874//===----------------------------------------------------------------------===//
1875
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001876void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001877 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001878 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001879 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Ted Kremenek451ac092009-08-06 04:50:20 +00001881 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001882 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001883}