blob: 4a84eea42f5476d4362e7be1f178d84584440064 [file] [log] [blame]
Zhongxing Xu17892752008-10-08 02:50:44 +00001//== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a basic region store model. In this model, we do have field
11// sensitivity. But we assume nothing about the heap shape. So recursive data
12// structures are largely ignored. Basically we do 1-limiting analysis.
13// Parameter pointers are assumed with no aliasing. Pointee objects of
14// parameters are created lazily.
15//
16//===----------------------------------------------------------------------===//
17#include "clang/Analysis/PathSensitive/MemRegion.h"
Zhongxing Xu0b143312009-08-21 13:25:15 +000018#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000019#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000020#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekd4e5a602009-08-06 21:43:54 +000022#include "clang/Analysis/Support/Optional.h"
Zhongxing Xu41fd0182009-05-06 11:51:48 +000023#include "clang/Basic/TargetInfo.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000024
25#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000026#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000027#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000028
29using namespace clang;
30
Ted Kremenek356e9d62009-07-22 04:35:42 +000031#define HEAP_UNDEFINED 0
Ted Kremeneka5e81f12009-08-06 01:20:57 +000032#define USE_EXPLICIT_COMPOUND 0
Ted Kremenek356e9d62009-07-22 04:35:42 +000033
Zhongxing Xu13d50172009-10-11 08:08:02 +000034namespace {
35class BindingVal {
36public:
37 enum BindingKind { Direct, Default };
38private:
39 SVal Value;
40 BindingKind Kind;
41
42public:
43 BindingVal(SVal V, BindingKind K) : Value(V), Kind(K) {}
44
45 bool isDefault() const { return Kind == Default; }
46
47 const SVal *getValue() const { return &Value; }
48
49 const SVal *getDirectValue() const { return isDefault() ? 0 : &Value; }
50
51 const SVal *getDefaultValue() const { return isDefault() ? &Value : 0; }
52
53 void Profile(llvm::FoldingSetNodeID& ID) const {
54 Value.Profile(ID);
55 ID.AddInteger(Kind);
56 }
57
58 inline bool operator==(const BindingVal& R) const {
59 return Value == R.Value && Kind == R.Kind;
60 }
61
62 inline bool operator!=(const BindingVal& R) const {
63 return !(*this == R);
64 }
65};
66}
67
68namespace llvm {
69static inline
70llvm::raw_ostream& operator<<(llvm::raw_ostream& os, BindingVal V) {
71 if (V.isDefault())
72 os << "(default) ";
73 else
74 os << "(direct) ";
75 os << *V.getValue();
76 return os;
77}
78} // end llvm namespace
79
Zhongxing Xubaf03a72008-11-24 09:44:56 +000080// Actual Store type.
Zhongxing Xu13d50172009-10-11 08:08:02 +000081typedef llvm::ImmutableMap<const MemRegion*, BindingVal> RegionBindings;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000082
Ted Kremenek50dc1b32008-12-24 01:05:03 +000083//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +000084// Fine-grained control of RegionStoreManager.
85//===----------------------------------------------------------------------===//
86
87namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000088struct minimal_features_tag {};
89struct maximal_features_tag {};
Mike Stump1eb44332009-09-09 15:08:12 +000090
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000091class RegionStoreFeatures {
Ted Kremenek9af46f52009-06-16 22:36:44 +000092 bool SupportsFields;
93 bool SupportsRemaining;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Ted Kremenek9af46f52009-06-16 22:36:44 +000095public:
96 RegionStoreFeatures(minimal_features_tag) :
97 SupportsFields(false), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek9af46f52009-06-16 22:36:44 +000099 RegionStoreFeatures(maximal_features_tag) :
100 SupportsFields(true), SupportsRemaining(false) {}
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek9af46f52009-06-16 22:36:44 +0000102 void enableFields(bool t) { SupportsFields = t; }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek9af46f52009-06-16 22:36:44 +0000104 bool supportsFields() const { return SupportsFields; }
105 bool supportsRemaining() const { return SupportsRemaining; }
106};
107}
108
109//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000110// Region "Extents"
111//===----------------------------------------------------------------------===//
112//
113// MemRegions represent chunks of memory with a size (their "extent"). This
114// GDM entry tracks the extents for regions. Extents are in bytes.
Ted Kremenekd6cfbe42009-01-07 22:18:50 +0000115//
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000116namespace { class RegionExtents {}; }
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000117static int RegionExtentsIndex = 0;
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000118namespace clang {
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000119 template<> struct GRStateTrait<RegionExtents>
120 : public GRStatePartialTrait<llvm::ImmutableMap<const MemRegion*, SVal> > {
121 static void* GDMIndex() { return &RegionExtentsIndex; }
122 };
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000123}
124
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000125//===----------------------------------------------------------------------===//
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000126// Utility functions.
127//===----------------------------------------------------------------------===//
128
129static bool IsAnyPointerOrIntptr(QualType ty, ASTContext &Ctx) {
130 if (ty->isAnyPointerType())
131 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000133 return ty->isIntegerType() && ty->isScalarType() &&
134 Ctx.getTypeSize(ty) == Ctx.getTypeSize(Ctx.VoidPtrTy);
135}
136
137//===----------------------------------------------------------------------===//
Ted Kremenek50dc1b32008-12-24 01:05:03 +0000138// Main RegionStore logic.
139//===----------------------------------------------------------------------===//
Ted Kremenekc48ea6e2008-12-04 02:08:27 +0000140
Zhongxing Xu17892752008-10-08 02:50:44 +0000141namespace {
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000143class RegionStoreSubRegionMap : public SubRegionMap {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000144 typedef llvm::ImmutableSet<const MemRegion*> SetTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000145 typedef llvm::DenseMap<const MemRegion*, SetTy> Map;
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000146 SetTy::Factory F;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000147 Map M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000148public:
Ted Kremenekd8c01922009-08-05 19:09:24 +0000149 bool add(const MemRegion* Parent, const MemRegion* SubRegion) {
Ted Kremenek59e8f112009-03-03 01:35:36 +0000150 Map::iterator I = M.find(Parent);
Ted Kremenekd8c01922009-08-05 19:09:24 +0000151
152 if (I == M.end()) {
Ted Kremenek4ed45982009-08-05 05:31:02 +0000153 M.insert(std::make_pair(Parent, F.Add(F.GetEmptySet(), SubRegion)));
Ted Kremenekd8c01922009-08-05 19:09:24 +0000154 return true;
155 }
156
157 I->second = F.Add(I->second, SubRegion);
158 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000159 }
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000161 void process(llvm::SmallVectorImpl<const SubRegion*> &WL, const SubRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Ted Kremenek59e8f112009-03-03 01:35:36 +0000163 ~RegionStoreSubRegionMap() {}
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek5dc27462009-03-03 02:51:43 +0000165 bool iterSubRegions(const MemRegion* Parent, Visitor& V) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000166 Map::const_iterator I = M.find(Parent);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000167
168 if (I == M.end())
Ted Kremenek5dc27462009-03-03 02:51:43 +0000169 return true;
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremenek59e8f112009-03-03 01:35:36 +0000171 llvm::ImmutableSet<const MemRegion*> S = I->second;
172 for (llvm::ImmutableSet<const MemRegion*>::iterator SI=S.begin(),SE=S.end();
173 SI != SE; ++SI) {
174 if (!V.Visit(Parent, *SI))
Ted Kremenek5dc27462009-03-03 02:51:43 +0000175 return false;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000176 }
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Ted Kremenek5dc27462009-03-03 02:51:43 +0000178 return true;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000179 }
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000181 typedef SetTy::iterator iterator;
182
183 std::pair<iterator, iterator> begin_end(const MemRegion *R) {
184 Map::iterator I = M.find(R);
185 SetTy S = I == M.end() ? F.GetEmptySet() : I->second;
186 return std::make_pair(S.begin(), S.end());
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188};
Ted Kremenek59e8f112009-03-03 01:35:36 +0000189
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +0000190class RegionStoreManager : public StoreManager {
Ted Kremenek9af46f52009-06-16 22:36:44 +0000191 const RegionStoreFeatures Features;
Ted Kremenek451ac092009-08-06 04:50:20 +0000192 RegionBindings::Factory RBFactory;
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000193
194 typedef llvm::DenseMap<const GRState *, RegionStoreSubRegionMap*> SMCache;
195 SMCache SC;
196
Zhongxing Xu17892752008-10-08 02:50:44 +0000197public:
Mike Stump1eb44332009-09-09 15:08:12 +0000198 RegionStoreManager(GRStateManager& mgr, const RegionStoreFeatures &f)
Ted Kremenekf7a0cf42009-07-29 21:43:22 +0000199 : StoreManager(mgr),
Ted Kremenek9af46f52009-06-16 22:36:44 +0000200 Features(f),
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000201 RBFactory(mgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +0000202
Ted Kremenek9e17cc62009-09-29 06:35:00 +0000203 virtual ~RegionStoreManager() {
204 for (SMCache::iterator I = SC.begin(), E = SC.end(); I != E; ++I)
205 delete (*I).second;
206 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000207
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000208 SubRegionMap *getSubRegionMap(const GRState *state);
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Zhongxing Xu13d50172009-10-11 08:08:02 +0000210 RegionStoreSubRegionMap *getRegionStoreSubRegionMap(Store store);
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Zhongxing Xu13d50172009-10-11 08:08:02 +0000212 Optional<SVal> getBinding(RegionBindings B, const MemRegion *R);
213 Optional<SVal> getDirectBinding(RegionBindings B, const MemRegion *R);
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000214 /// getDefaultBinding - Returns an SVal* representing an optional default
215 /// binding associated with a region and its subregions.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000216 Optional<SVal> getDefaultBinding(RegionBindings B, const MemRegion *R);
Ted Kremenek027e2662009-11-19 20:20:24 +0000217
218 /// setImplicitDefaultValue - Set the default binding for the provided
219 /// MemRegion to the value implicitly defined for compound literals when
220 /// the value is not specified.
221 const GRState *setImplicitDefaultValue(const GRState *state,
222 const MemRegion *R,
223 QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000224
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000225 /// getLValueString - Returns an SVal representing the lvalue of a
226 /// StringLiteral. Within RegionStore a StringLiteral has an
227 /// associated StringRegion, and the lvalue of a StringLiteral is
228 /// the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000229 SVal getLValueString(const StringLiteral* S);
Zhongxing Xu143bf822008-10-25 14:18:57 +0000230
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000231 /// getLValueCompoundLiteral - Returns an SVal representing the
232 /// lvalue of a compound literal. Within RegionStore a compound
233 /// literal has an associated region, and the lvalue of the
234 /// compound literal is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000235 SVal getLValueCompoundLiteral(const CompoundLiteralExpr*);
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000236
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000237 /// getLValueVar - Returns an SVal that represents the lvalue of a
238 /// variable. Within RegionStore a variable has an associated
239 /// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000240 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC);
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000242 SVal getLValueIvar(const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000243
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000244 SVal getLValueField(const FieldDecl* D, SVal Base);
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000246 SVal getLValueFieldOrIvar(const Decl* D, SVal Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000247
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000248 SVal getLValueElement(QualType elementType, SVal Offset, SVal Base);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000249
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000250
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000251 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
252 /// type. 'Array' represents the lvalue of the array being decayed
253 /// to a pointer, and the returned SVal represents the decayed
254 /// version of that lvalue (i.e., a pointer to the first element of
255 /// the array). This is called by GRExprEngine when evaluating
256 /// casts from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000257 SVal ArrayToPointer(Loc Array);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000258
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000259 SVal EvalBinOp(const GRState *state, BinaryOperator::Opcode Op,Loc L,
Ted Kremenek5c734622009-06-26 00:41:43 +0000260 NonLoc R, QualType resultTy);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000261
Mike Stump1eb44332009-09-09 15:08:12 +0000262 Store getInitialStore(const LocationContext *InitLoc) {
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000263 return RBFactory.GetEmptyMap().getRoot();
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000264 }
Ted Kremenek82cd37c2009-08-21 23:25:54 +0000265
Ted Kremenek67f28532009-06-17 22:02:04 +0000266 //===-------------------------------------------------------------------===//
267 // Binding values to regions.
268 //===-------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000269
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000270 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
Ted Kremenek473e1672009-10-16 00:30:49 +0000271 const Expr *E, unsigned Count,
Ted Kremenek81a95832009-12-03 03:27:11 +0000272 InvalidatedSymbols *IS) {
273 return RegionStoreManager::InvalidateRegions(state, &R, &R+1, E, Count, IS);
274 }
275
276 const GRState *InvalidateRegions(const GRState *state,
277 const MemRegion * const *Begin,
278 const MemRegion * const *End,
279 const Expr *E, unsigned Count,
280 InvalidatedSymbols *IS);
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000282private:
Zhongxing Xu13d50172009-10-11 08:08:02 +0000283 void RemoveSubRegionBindings(RegionBindings &B, const MemRegion *R,
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000284 RegionStoreSubRegionMap &M);
Mike Stump1eb44332009-09-09 15:08:12 +0000285
286public:
Ted Kremenek67f28532009-06-17 22:02:04 +0000287 const GRState *Bind(const GRState *state, Loc LV, SVal V);
288
289 const GRState *BindCompoundLiteral(const GRState *state,
Zhongxing Xu13d50172009-10-11 08:08:02 +0000290 const CompoundLiteralExpr* CL, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000292 const GRState *BindDecl(const GRState *ST, const VarRegion *VR,
293 SVal InitVal);
Ted Kremenek67f28532009-06-17 22:02:04 +0000294
Ted Kremenekf6f56d42009-11-04 00:09:15 +0000295 const GRState *BindDeclWithNoInit(const GRState *state,
296 const VarRegion *) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000297 return state;
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000298 }
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000299
Ted Kremenek67f28532009-06-17 22:02:04 +0000300 /// BindStruct - Bind a compound value to a structure.
301 const GRState *BindStruct(const GRState *, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000302
Ted Kremenek67f28532009-06-17 22:02:04 +0000303 const GRState *BindArray(const GRState *state, const TypedRegion* R, SVal V);
Mike Stump1eb44332009-09-09 15:08:12 +0000304
305 /// KillStruct - Set the entire struct to unknown.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000306 Store KillStruct(Store store, const TypedRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000307
Ted Kremenek67f28532009-06-17 22:02:04 +0000308 Store Remove(Store store, Loc LV);
309
310 //===------------------------------------------------------------------===//
311 // Loading values from regions.
312 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Ted Kremenek67f28532009-06-17 22:02:04 +0000314 /// The high level logic for this method is this:
315 /// Retrieve (L)
316 /// if L has binding
317 /// return L's binding
318 /// else if L is in killset
319 /// return unknown
320 /// else
321 /// if L is on stack or heap
322 /// return undefined
323 /// else
324 /// return symbolic
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000325 SValuator::CastResult Retrieve(const GRState *state, Loc L,
326 QualType T = QualType());
Zhongxing Xu490b0f02009-06-25 04:50:44 +0000327
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000328 SVal RetrieveElement(const GRState *state, const ElementRegion *R);
Zhongxing Xuc00346f2009-06-25 05:29:39 +0000329
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000330 SVal RetrieveField(const GRState *state, const FieldRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Ted Kremenek5bd2fe32009-07-15 06:09:28 +0000332 SVal RetrieveObjCIvar(const GRState *state, const ObjCIvarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Ted Kremenek9031dd72009-07-21 00:12:07 +0000334 SVal RetrieveVar(const GRState *state, const VarRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Ted Kremenek25c54572009-07-20 22:58:02 +0000336 SVal RetrieveLazySymbol(const GRState *state, const TypedRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Ted Kremenek566a6fa2009-08-06 22:33:36 +0000338 SVal RetrieveFieldOrElementCommon(const GRState *state, const TypedRegion *R,
339 QualType Ty, const MemRegion *superR);
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Ted Kremenek67f28532009-06-17 22:02:04 +0000341 /// Retrieve the values in a struct and return a CompoundVal, used when doing
Mike Stump1eb44332009-09-09 15:08:12 +0000342 /// struct copy:
343 /// struct s x, y;
Ted Kremenek67f28532009-06-17 22:02:04 +0000344 /// x = y;
345 /// y's value is retrieved by this method.
346 SVal RetrieveStruct(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Ted Kremenek67f28532009-06-17 22:02:04 +0000348 SVal RetrieveArray(const GRState *St, const TypedRegion* R);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000350 std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +0000351 GetLazyBinding(RegionBindings B, const MemRegion *R);
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000353 const GRState* CopyLazyBindings(nonloc::LazyCompoundVal V,
354 const GRState *state,
355 const TypedRegion *R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000356
Ted Kremenek0954cde2009-09-24 04:11:44 +0000357 const ElementRegion *GetElementZeroRegion(const SymbolicRegion *SR,
358 QualType T);
359
Ted Kremenek67f28532009-06-17 22:02:04 +0000360 //===------------------------------------------------------------------===//
361 // State pruning.
362 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Ted Kremenek67f28532009-06-17 22:02:04 +0000364 /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
365 /// It returns a new Store with these values removed.
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000366 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
Ted Kremenek67f28532009-06-17 22:02:04 +0000367 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
368
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +0000369 const GRState *EnterStackFrame(const GRState *state,
370 const StackFrameContext *frame);
371
Ted Kremenek67f28532009-06-17 22:02:04 +0000372 //===------------------------------------------------------------------===//
373 // Region "extents".
374 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Ted Kremenek67f28532009-06-17 22:02:04 +0000376 const GRState *setExtent(const GRState *state, const MemRegion* R, SVal Extent);
Zhongxing Xue884ff82009-11-12 02:48:32 +0000377 DefinedOrUnknownSVal getSizeInElements(const GRState *state,
378 const MemRegion* R);
Ted Kremenek67f28532009-06-17 22:02:04 +0000379
380 //===------------------------------------------------------------------===//
Ted Kremenek67f28532009-06-17 22:02:04 +0000381 // Utility methods.
382 //===------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Ted Kremenek451ac092009-08-06 04:50:20 +0000384 static inline RegionBindings GetRegionBindings(Store store) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000385 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
Zhongxing Xu17892752008-10-08 02:50:44 +0000386 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000387
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000388 void print(Store store, llvm::raw_ostream& Out, const char* nl,
389 const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000390
391 void iterBindings(Store store, BindingsHandler& f) {
392 // FIXME: Implement.
393 }
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000394
Ted Kremenek67f28532009-06-17 22:02:04 +0000395 // FIXME: Remove.
396 BasicValueFactory& getBasicVals() {
397 return StateMgr.getBasicVals();
398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek67f28532009-06-17 22:02:04 +0000400 // FIXME: Remove.
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000401 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu17892752008-10-08 02:50:44 +0000402};
403
404} // end anonymous namespace
405
Ted Kremenek9af46f52009-06-16 22:36:44 +0000406//===----------------------------------------------------------------------===//
407// RegionStore creation.
408//===----------------------------------------------------------------------===//
409
410StoreManager *clang::CreateRegionStoreManager(GRStateManager& StMgr) {
411 RegionStoreFeatures F = maximal_features_tag();
412 return new RegionStoreManager(StMgr, F);
413}
414
415StoreManager *clang::CreateFieldsOnlyRegionStoreManager(GRStateManager &StMgr) {
416 RegionStoreFeatures F = minimal_features_tag();
417 F.enableFields(true);
418 return new RegionStoreManager(StMgr, F);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000419}
420
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000421void
422RegionStoreSubRegionMap::process(llvm::SmallVectorImpl<const SubRegion*> &WL,
Mike Stump1eb44332009-09-09 15:08:12 +0000423 const SubRegion *R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000424 const MemRegion *superR = R->getSuperRegion();
425 if (add(superR, R))
426 if (const SubRegion *sr = dyn_cast<SubRegion>(superR))
Mike Stump1eb44332009-09-09 15:08:12 +0000427 WL.push_back(sr);
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000428}
429
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000430RegionStoreSubRegionMap*
Zhongxing Xu13d50172009-10-11 08:08:02 +0000431RegionStoreManager::getRegionStoreSubRegionMap(Store store) {
432 RegionBindings B = GetRegionBindings(store);
Ted Kremenek59e8f112009-03-03 01:35:36 +0000433 RegionStoreSubRegionMap *M = new RegionStoreSubRegionMap();
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000435 llvm::SmallVector<const SubRegion*, 10> WL;
436
Ted Kremenek451ac092009-08-06 04:50:20 +0000437 for (RegionBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I)
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000438 if (const SubRegion *R = dyn_cast<SubRegion>(I.getKey()))
439 M->process(WL, R);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Mike Stump1eb44332009-09-09 15:08:12 +0000441 // We also need to record in the subregion map "intermediate" regions that
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000442 // don't have direct bindings but are super regions of those that do.
443 while (!WL.empty()) {
444 const SubRegion *R = WL.back();
445 WL.pop_back();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000446 M->process(WL, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000447 }
448
Ted Kremenek14453bf2009-03-03 19:02:42 +0000449 return M;
Ted Kremenek59e8f112009-03-03 01:35:36 +0000450}
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000451
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000452SubRegionMap *RegionStoreManager::getSubRegionMap(const GRState *state) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000453 return getRegionStoreSubRegionMap(state->getStore());
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000454}
455
Ted Kremenek9af46f52009-06-16 22:36:44 +0000456//===----------------------------------------------------------------------===//
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000457// Binding invalidation.
458//===----------------------------------------------------------------------===//
459
Zhongxing Xu13d50172009-10-11 08:08:02 +0000460void RegionStoreManager::RemoveSubRegionBindings(RegionBindings &B,
461 const MemRegion *R,
462 RegionStoreSubRegionMap &M) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000463 RegionStoreSubRegionMap::iterator I, E;
464
465 for (llvm::tie(I, E) = M.begin_end(R); I != E; ++I)
Zhongxing Xu13d50172009-10-11 08:08:02 +0000466 RemoveSubRegionBindings(B, *I, M);
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000468 B = RBFactory.Remove(B, R);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +0000469}
470
Ted Kremenek81a95832009-12-03 03:27:11 +0000471const GRState *RegionStoreManager::InvalidateRegions(const GRState *state,
472 const MemRegion * const *I,
473 const MemRegion * const *E,
474 const Expr *Ex,
475 unsigned Count,
476 InvalidatedSymbols *IS) {
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000477 ASTContext& Ctx = StateMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Ted Kremenek87806792009-09-27 20:45:21 +0000479 // Get the mapping of regions -> subregions.
480 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +0000481 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremenek87806792009-09-27 20:45:21 +0000482
483 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000484
Ted Kremenek87806792009-09-27 20:45:21 +0000485 llvm::DenseMap<const MemRegion *, unsigned> Visited;
486 llvm::SmallVector<const MemRegion *, 10> WorkList;
Ted Kremenek81a95832009-12-03 03:27:11 +0000487
488 for ( ; I != E; ++I) {
489 // Strip away casts.
490 WorkList.push_back((*I)->StripCasts());
491 }
Ted Kremenek87806792009-09-27 20:45:21 +0000492
493 while (!WorkList.empty()) {
Ted Kremenek81a95832009-12-03 03:27:11 +0000494 const MemRegion *R = WorkList.back();
Ted Kremenek87806792009-09-27 20:45:21 +0000495 WorkList.pop_back();
496
497 // Have we visited this region before?
498 unsigned &visited = Visited[R];
499 if (visited)
500 continue;
501 visited = 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Ted Kremenek87806792009-09-27 20:45:21 +0000503 // Add subregions to work list.
504 RegionStoreSubRegionMap::iterator I, E;
505 for (llvm::tie(I, E) = SubRegions->begin_end(R); I!=E; ++I)
506 WorkList.push_back(*I);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000507
508 // Get the old binding. Is it a region? If so, add it to the worklist.
509 if (Optional<SVal> V = getDirectBinding(B, R)) {
510 if (const MemRegion *RV = V->getAsRegion())
511 WorkList.push_back(RV);
Ted Kremenek473e1672009-10-16 00:30:49 +0000512
513 // A symbol? Mark it touched by the invalidation.
514 if (IS) {
515 if (SymbolRef Sym = V->getAsSymbol())
516 IS->insert(Sym);
517 }
Zhongxing Xu13d50172009-10-11 08:08:02 +0000518 }
519
Ted Kremenek473e1672009-10-16 00:30:49 +0000520 // Symbolic region? Mark that symbol touched by the invalidation.
521 if (IS) {
522 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
523 IS->insert(SR->getSymbol());
524 }
Ted Kremenek2ffbfd92009-12-03 08:25:47 +0000525
526 // BlockDataRegion? If so, invalidate captured variables that are passed
527 // by reference.
528 if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(R)) {
529 for (BlockDataRegion::referenced_vars_iterator
530 I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
531 I != E; ++I) {
532 const VarRegion *VR = *I;
533 if (VR->getDecl()->getAttr<BlocksAttr>())
534 WorkList.push_back(VR);
535 }
536 continue;
537 }
Ted Kremenek473e1672009-10-16 00:30:49 +0000538
539 // Handle the region itself.
Ted Kremenek87806792009-09-27 20:45:21 +0000540 if (isa<AllocaRegion>(R) || isa<SymbolicRegion>(R) ||
541 isa<ObjCObjectRegion>(R)) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000542 // Invalidate the region by setting its default value to
543 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000544 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
545 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000546 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000547 continue;
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Ted Kremenek87806792009-09-27 20:45:21 +0000550 if (!R->isBoundable())
551 continue;
552
553 const TypedRegion *TR = cast<TypedRegion>(R);
554 QualType T = TR->getValueType(Ctx);
555
556 if (const RecordType *RT = T->getAsStructureType()) {
Ted Kremenek87806792009-09-27 20:45:21 +0000557 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
558
Zhongxing Xu13d50172009-10-11 08:08:02 +0000559 // No record definition. There is nothing we can do.
Ted Kremenek87806792009-09-27 20:45:21 +0000560 if (!RD)
561 continue;
562
Zhongxing Xu13d50172009-10-11 08:08:02 +0000563 // Invalidate the region by setting its default value to
564 // conjured symbol. The type of the symbol is irrelavant.
Ted Kremenek87806792009-09-27 20:45:21 +0000565 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, Ctx.IntTy,
566 Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000567 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000568 continue;
569 }
570
571 if (const ArrayType *AT = Ctx.getAsArrayType(T)) {
572 // Set the default value of the array to conjured symbol.
573 DefinedOrUnknownSVal V =
574 ValMgr.getConjuredSymbolVal(R, Ex, AT->getElementType(), Count);
Zhongxing Xu13d50172009-10-11 08:08:02 +0000575 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
Ted Kremenek87806792009-09-27 20:45:21 +0000576 continue;
577 }
Ted Kremeneka5971b32009-09-29 03:34:03 +0000578
579 if ((isa<FieldRegion>(R)||isa<ElementRegion>(R)||isa<ObjCIvarRegion>(R))
580 && Visited[cast<SubRegion>(R)->getSuperRegion()]) {
Zhongxing Xu13d50172009-10-11 08:08:02 +0000581 // For fields and elements whose super region has also been invalidated,
582 // only remove the old binding. The super region will get set with a
583 // default value from which we can lazily derive a new symbolic value.
Ted Kremeneka5971b32009-09-29 03:34:03 +0000584 B = RBFactory.Remove(B, R);
585 continue;
586 }
Ted Kremenek87806792009-09-27 20:45:21 +0000587
Ted Kremenek389c44c2009-09-29 03:12:50 +0000588 // Invalidate the binding.
Ted Kremenek87806792009-09-27 20:45:21 +0000589 DefinedOrUnknownSVal V = ValMgr.getConjuredSymbolVal(R, Ex, T, Count);
590 assert(SymbolManager::canSymbolicate(T) || V.isUnknown());
Zhongxing Xu13d50172009-10-11 08:08:02 +0000591 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct));
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000592 }
593
Ted Kremenek87806792009-09-27 20:45:21 +0000594 // Create a new state with the updated bindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +0000595 return state->makeWithStore(B.getRoot());
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000596}
597
598//===----------------------------------------------------------------------===//
Ted Kremenek9af46f52009-06-16 22:36:44 +0000599// getLValueXXX methods.
600//===----------------------------------------------------------------------===//
601
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000602/// getLValueString - Returns an SVal representing the lvalue of a
603/// StringLiteral. Within RegionStore a StringLiteral has an
604/// associated StringRegion, and the lvalue of a StringLiteral is the
605/// lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000606SVal RegionStoreManager::getLValueString(const StringLiteral* S) {
Zhongxing Xu143bf822008-10-25 14:18:57 +0000607 return loc::MemRegionVal(MRMgr.getStringRegion(S));
608}
609
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000610/// getLValueVar - Returns an SVal that represents the lvalue of a
611/// variable. Within RegionStore a variable has an associated
612/// VarRegion, and the lvalue of the variable is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000613SVal RegionStoreManager::getLValueVar(const VarDecl *VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000614 const LocationContext *LC) {
615 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000616}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000617
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000618/// getLValueCompoundLiteral - Returns an SVal representing the lvalue
619/// of a compound literal. Within RegionStore a compound literal
620/// has an associated region, and the lvalue of the compound literal
621/// is the lvalue of that region.
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000622SVal
623RegionStoreManager::getLValueCompoundLiteral(const CompoundLiteralExpr* CL) {
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000624 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
625}
626
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000627SVal RegionStoreManager::getLValueIvar(const ObjCIvarDecl* D, SVal Base) {
628 return getLValueFieldOrIvar(D, Base);
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000629}
630
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000631SVal RegionStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
632 return getLValueFieldOrIvar(D, Base);
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000633}
634
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000635SVal RegionStoreManager::getLValueFieldOrIvar(const Decl* D, SVal Base) {
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000636 if (Base.isUnknownOrUndef())
637 return Base;
638
639 Loc BaseL = cast<Loc>(Base);
640 const MemRegion* BaseR = 0;
641
642 switch (BaseL.getSubKind()) {
643 case loc::MemRegionKind:
644 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
645 break;
646
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000647 case loc::GotoLabelKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000648 // These are anormal cases. Flag an undefined value.
649 return UndefinedVal();
650
651 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000652 // While these seem funny, this can happen through casts.
653 // FIXME: What we should return is the field offset. For example,
654 // add the field offset to the integer value. That way funny things
655 // like this work properly: &(((struct foo *) 0xa)->f)
656 return Base;
657
658 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000659 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000660 return Base;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000663 // NOTE: We must have this check first because ObjCIvarDecl is a subclass
664 // of FieldDecl.
665 if (const ObjCIvarDecl *ID = dyn_cast<ObjCIvarDecl>(D))
666 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000667
Ted Kremenek3de2d3c2009-03-05 04:50:08 +0000668 return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000669}
670
Zhongxing Xud0f8bb12009-10-14 03:33:08 +0000671SVal RegionStoreManager::getLValueElement(QualType elementType, SVal Offset,
672 SVal Base) {
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000673
Ted Kremenekde7ec632009-03-09 22:44:49 +0000674 // If the base is an unknown or undefined value, just return it back.
675 // FIXME: For absolute pointer addresses, we just return that value back as
676 // well, although in reality we should return the offset added to that
677 // value.
678 if (Base.isUnknownOrUndef() || isa<loc::ConcreteInt>(Base))
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000679 return Base;
680
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000681 // Only handle integer offsets... for now.
682 if (!isa<nonloc::ConcreteInt>(Offset))
Zhongxing Xue4d13932008-11-13 09:48:44 +0000683 return UnknownVal();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000684
Zhongxing Xuce760782009-05-09 13:20:07 +0000685 const MemRegion* BaseRegion = cast<loc::MemRegionVal>(Base).getRegion();
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000686
687 // Pointer of any type can be cast and used as array base.
688 const ElementRegion *ElemR = dyn_cast<ElementRegion>(BaseRegion);
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Ted Kremenek46537392009-07-16 01:33:37 +0000690 // Convert the offset to the appropriate size and signedness.
691 Offset = ValMgr.convertToArrayIndex(Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000693 if (!ElemR) {
694 //
695 // If the base region is not an ElementRegion, create one.
696 // This can happen in the following example:
697 //
698 // char *p = __builtin_alloc(10);
699 // p[1] = 8;
700 //
Zhongxing Xuce760782009-05-09 13:20:07 +0000701 // Observe that 'p' binds to an AllocaRegion.
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000702 //
Ted Kremenekf936f452009-05-04 06:18:28 +0000703 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000704 BaseRegion, getContext()));
Zhongxing Xue4d13932008-11-13 09:48:44 +0000705 }
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000707 SVal BaseIdx = ElemR->getIndex();
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000709 if (!isa<nonloc::ConcreteInt>(BaseIdx))
710 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Ted Kremeneka7ac9442009-01-22 20:27:48 +0000712 const llvm::APSInt& BaseIdxI = cast<nonloc::ConcreteInt>(BaseIdx).getValue();
713 const llvm::APSInt& OffI = cast<nonloc::ConcreteInt>(Offset).getValue();
714 assert(BaseIdxI.isSigned());
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Ted Kremenek46537392009-07-16 01:33:37 +0000716 // Compute the new index.
717 SVal NewIdx = nonloc::ConcreteInt(getBasicVals().getValue(BaseIdxI + OffI));
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Ted Kremenek46537392009-07-16 01:33:37 +0000719 // Construct the new ElementRegion.
720 const MemRegion *ArrayR = ElemR->getSuperRegion();
Zhongxing Xu143b2fc2009-06-16 09:55:50 +0000721 return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
Mike Stump1eb44332009-09-09 15:08:12 +0000722 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000723}
724
Ted Kremenek9af46f52009-06-16 22:36:44 +0000725//===----------------------------------------------------------------------===//
726// Extents for regions.
727//===----------------------------------------------------------------------===//
728
Zhongxing Xue884ff82009-11-12 02:48:32 +0000729DefinedOrUnknownSVal RegionStoreManager::getSizeInElements(const GRState *state,
730 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000732 switch (R->getKind()) {
733 case MemRegion::MemSpaceRegionKind:
734 assert(0 && "Cannot index into a MemSpace");
Mike Stump1eb44332009-09-09 15:08:12 +0000735 return UnknownVal();
736
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000737 case MemRegion::FunctionTextRegionKind:
738 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000739 case MemRegion::BlockDataRegionKind:
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000740 // Technically this can happen if people do funny things with casts.
Ted Kremenek14553ab2009-01-30 00:08:43 +0000741 return UnknownVal();
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000742
743 // Not yet handled.
744 case MemRegion::AllocaRegionKind:
745 case MemRegion::CompoundLiteralRegionKind:
746 case MemRegion::ElementRegionKind:
747 case MemRegion::FieldRegionKind:
748 case MemRegion::ObjCIvarRegionKind:
749 case MemRegion::ObjCObjectRegionKind:
750 case MemRegion::SymbolicRegionKind:
751 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000753 case MemRegion::StringRegionKind: {
754 const StringLiteral* Str = cast<StringRegion>(R)->getStringLiteral();
Mike Stump1eb44332009-09-09 15:08:12 +0000755 // We intentionally made the size value signed because it participates in
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000756 // operations with signed indices.
757 return ValMgr.makeIntVal(Str->getByteLength()+1, false);
Ted Kremenek14553ab2009-01-30 00:08:43 +0000758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000760 case MemRegion::VarRegionKind: {
761 const VarRegion* VR = cast<VarRegion>(R);
762 // Get the type of the variable.
763 QualType T = VR->getDesugaredValueType(getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000765 // FIXME: Handle variable-length arrays.
766 if (isa<VariableArrayType>(T))
767 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000769 if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T)) {
770 // return the size as signed integer.
771 return ValMgr.makeIntVal(CAT->getSize(), false);
772 }
Ted Kremenekdf74e252009-08-02 05:15:23 +0000773
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000774 // Clients can use ordinary variables as if they were arrays. These
775 // essentially are arrays of size 1.
776 return ValMgr.makeIntVal(1, false);
Zhongxing Xu41fd0182009-05-06 11:51:48 +0000777 }
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000779 case MemRegion::BEG_DECL_REGIONS:
780 case MemRegion::END_DECL_REGIONS:
781 case MemRegion::BEG_TYPED_REGIONS:
782 case MemRegion::END_TYPED_REGIONS:
783 assert(0 && "Infeasible region");
784 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Ted Kremenek7ecbfbc2009-07-10 22:30:06 +0000787 assert(0 && "Unreachable");
Ted Kremeneka21362d2009-01-06 19:12:06 +0000788 return UnknownVal();
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000789}
790
Ted Kremenek67f28532009-06-17 22:02:04 +0000791const GRState *RegionStoreManager::setExtent(const GRState *state,
792 const MemRegion *region,
793 SVal extent) {
794 return state->set<RegionExtents>(region, extent);
Ted Kremenek9af46f52009-06-16 22:36:44 +0000795}
796
797//===----------------------------------------------------------------------===//
798// Location and region casting.
799//===----------------------------------------------------------------------===//
800
Ted Kremenek869fb4a2008-12-24 07:46:32 +0000801/// ArrayToPointer - Emulates the "decay" of an array to a pointer
802/// type. 'Array' represents the lvalue of the array being decayed
803/// to a pointer, and the returned SVal represents the decayed
804/// version of that lvalue (i.e., a pointer to the first element of
805/// the array). This is called by GRExprEngine when evaluating casts
806/// from arrays to pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +0000807SVal RegionStoreManager::ArrayToPointer(Loc Array) {
Ted Kremenekabb042f2008-12-13 19:24:37 +0000808 if (!isa<loc::MemRegionVal>(Array))
809 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Ted Kremenekabb042f2008-12-13 19:24:37 +0000811 const MemRegion* R = cast<loc::MemRegionVal>(&Array)->getRegion();
812 const TypedRegion* ArrayR = dyn_cast<TypedRegion>(R);
Mike Stump1eb44332009-09-09 15:08:12 +0000813
Ted Kremenekbbee1a72009-01-13 01:03:27 +0000814 if (!ArrayR)
Ted Kremenekabb042f2008-12-13 19:24:37 +0000815 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Zhongxing Xua82d8aa2009-05-09 03:57:34 +0000817 // Strip off typedefs from the ArrayRegion's ValueType.
John McCallbf1cc052009-09-29 23:03:30 +0000818 QualType T = ArrayR->getValueType(getContext()).getDesugaredType();
Ted Kremenekf936f452009-05-04 06:18:28 +0000819 ArrayType *AT = cast<ArrayType>(T);
820 T = AT->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Ted Kremenek75185b52009-07-16 00:00:11 +0000822 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
Ted Kremenekb48ad642009-12-04 00:26:31 +0000823 return loc::MemRegionVal(MRMgr.getElementRegion(T, ZeroIdx, ArrayR,
824 getContext()));
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000825}
826
Ted Kremenek9af46f52009-06-16 22:36:44 +0000827//===----------------------------------------------------------------------===//
828// Pointer arithmetic.
829//===----------------------------------------------------------------------===//
830
Mike Stump1eb44332009-09-09 15:08:12 +0000831SVal RegionStoreManager::EvalBinOp(const GRState *state,
Ted Kremenek5c734622009-06-26 00:41:43 +0000832 BinaryOperator::Opcode Op, Loc L, NonLoc R,
833 QualType resultTy) {
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000834 // Assume the base location is MemRegionVal.
Ted Kremenek5dc27462009-03-03 02:51:43 +0000835 if (!isa<loc::MemRegionVal>(L))
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000836 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000837
Zhongxing Xua1718c72009-04-03 07:33:13 +0000838 const MemRegion* MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xuc4761f52009-05-09 15:18:12 +0000839 const ElementRegion *ER = 0;
Zhongxing Xu262fd032009-05-20 09:00:16 +0000840
Ted Kremenek3bccf082009-07-11 00:58:27 +0000841 switch (MR->getKind()) {
842 case MemRegion::SymbolicRegionKind: {
843 const SymbolicRegion *SR = cast<SymbolicRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000844 SymbolRef Sym = SR->getSymbol();
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000845 QualType T = Sym->getType(getContext());
846 QualType EleTy;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Ted Kremenekbcf62a92009-08-25 22:55:09 +0000848 if (const PointerType *PT = T->getAs<PointerType>())
849 EleTy = PT->getPointeeType();
850 else
John McCall183700f2009-09-21 23:43:11 +0000851 EleTy = T->getAs<ObjCObjectPointerType>()->getPointeeType();
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Ted Kremenek3bccf082009-07-11 00:58:27 +0000853 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
854 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, SR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000855 break;
Zhongxing Xu005f07b2009-06-19 04:51:14 +0000856 }
Ted Kremenek3bccf082009-07-11 00:58:27 +0000857 case MemRegion::AllocaRegionKind: {
Ted Kremenek3bccf082009-07-11 00:58:27 +0000858 const AllocaRegion *AR = cast<AllocaRegion>(MR);
Ted Kremenekdf74e252009-08-02 05:15:23 +0000859 QualType T = getContext().CharTy; // Create an ElementRegion of bytes.
Ted Kremenek6217b802009-07-29 21:53:49 +0000860 QualType EleTy = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek3bccf082009-07-11 00:58:27 +0000861 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
862 ER = MRMgr.getElementRegion(EleTy, ZeroIdx, AR, getContext());
Mike Stump1eb44332009-09-09 15:08:12 +0000863 break;
Ted Kremenek3bccf082009-07-11 00:58:27 +0000864 }
Zhongxing Xua1718c72009-04-03 07:33:13 +0000865
Ted Kremenek3bccf082009-07-11 00:58:27 +0000866 case MemRegion::ElementRegionKind: {
867 ER = cast<ElementRegion>(MR);
868 break;
869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Ted Kremenek3bccf082009-07-11 00:58:27 +0000871 // Not yet handled.
872 case MemRegion::VarRegionKind:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000873 case MemRegion::StringRegionKind: {
874
875 }
876 // Fall-through.
Ted Kremenek3bccf082009-07-11 00:58:27 +0000877 case MemRegion::CompoundLiteralRegionKind:
878 case MemRegion::FieldRegionKind:
879 case MemRegion::ObjCObjectRegionKind:
880 case MemRegion::ObjCIvarRegionKind:
881 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Ted Kremenekeb1c7a02009-11-25 01:32:22 +0000883 case MemRegion::FunctionTextRegionKind:
884 case MemRegion::BlockTextRegionKind:
Ted Kremenek0a8112a2009-11-25 23:53:07 +0000885 case MemRegion::BlockDataRegionKind:
Ted Kremenek3bccf082009-07-11 00:58:27 +0000886 // Technically this can happen if people do funny things with casts.
887 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Ted Kremenek3bccf082009-07-11 00:58:27 +0000889 case MemRegion::MemSpaceRegionKind:
890 assert(0 && "Cannot perform pointer arithmetic on a MemSpace");
891 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Ted Kremenek3bccf082009-07-11 00:58:27 +0000893 case MemRegion::BEG_DECL_REGIONS:
894 case MemRegion::END_DECL_REGIONS:
895 case MemRegion::BEG_TYPED_REGIONS:
896 case MemRegion::END_TYPED_REGIONS:
897 assert(0 && "Infeasible region");
898 return UnknownVal();
Zhongxing Xu5414a5c2009-06-21 13:24:24 +0000899 }
Zhongxing Xu2b1dc172009-03-11 07:43:49 +0000900
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000901 SVal Idx = ER->getIndex();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000902 nonloc::ConcreteInt* Base = dyn_cast<nonloc::ConcreteInt>(&Idx);
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000903
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000904 // For now, only support:
905 // (a) concrete integer indices that can easily be resolved
906 // (b) 0 + symbolic index
907 if (Base) {
908 if (nonloc::ConcreteInt *Offset = dyn_cast<nonloc::ConcreteInt>(&R)) {
909 // FIXME: Should use SValuator here.
910 SVal NewIdx =
911 Base->evalBinOp(ValMgr, Op,
Ted Kremenek46537392009-07-16 01:33:37 +0000912 cast<nonloc::ConcreteInt>(ValMgr.convertToArrayIndex(*Offset)));
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000913 const MemRegion* NewER =
914 MRMgr.getElementRegion(ER->getElementType(), NewIdx,
915 ER->getSuperRegion(), getContext());
916 return ValMgr.makeLoc(NewER);
917 }
918 if (0 == Base->getValue()) {
919 const MemRegion* NewER =
920 MRMgr.getElementRegion(ER->getElementType(), R,
921 ER->getSuperRegion(), getContext());
922 return ValMgr.makeLoc(NewER);
923 }
Ted Kremenek5dc27462009-03-03 02:51:43 +0000924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Ted Kremenek5dc27462009-03-03 02:51:43 +0000926 return UnknownVal();
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000927}
928
Ted Kremenek9af46f52009-06-16 22:36:44 +0000929//===----------------------------------------------------------------------===//
930// Loading values from regions.
931//===----------------------------------------------------------------------===//
932
Zhongxing Xu13d50172009-10-11 08:08:02 +0000933Optional<SVal> RegionStoreManager::getDirectBinding(RegionBindings B,
934 const MemRegion *R) {
935 if (const BindingVal *BV = B.lookup(R))
936 return Optional<SVal>::create(BV->getDirectValue());
937
938 return Optional<SVal>();
939}
940
941Optional<SVal> RegionStoreManager::getDefaultBinding(RegionBindings B,
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000942 const MemRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000944 if (R->isBoundable())
945 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R))
946 if (TR->getValueType(getContext())->isUnionType())
947 return UnknownVal();
948
Zhongxing Xu13d50172009-10-11 08:08:02 +0000949 if (BindingVal const *V = B.lookup(R))
950 return Optional<SVal>::create(V->getDefaultValue());
951
952 return Optional<SVal>();
953}
954
955Optional<SVal> RegionStoreManager::getBinding(RegionBindings B,
956 const MemRegion *R) {
957 if (const BindingVal *BV = B.lookup(R))
958 return Optional<SVal>::create(BV->getValue());
959
960 return Optional<SVal>();
Ted Kremenekd4e5a602009-08-06 21:43:54 +0000961}
962
Ted Kremeneka6275a52009-07-15 02:31:43 +0000963static bool IsReinterpreted(QualType RTy, QualType UsedTy, ASTContext &Ctx) {
964 RTy = Ctx.getCanonicalType(RTy);
965 UsedTy = Ctx.getCanonicalType(UsedTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Ted Kremeneka6275a52009-07-15 02:31:43 +0000967 if (RTy == UsedTy)
968 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
970
Ted Kremenek25c54572009-07-20 22:58:02 +0000971 // Recursively check the types. We basically want to see if a pointer value
Mike Stump1eb44332009-09-09 15:08:12 +0000972 // is ever reinterpreted as a non-pointer, e.g. void** and intptr_t*
Ted Kremenek25c54572009-07-20 22:58:02 +0000973 // represents a reinterpretation.
974 if (Loc::IsLocType(RTy) && Loc::IsLocType(UsedTy)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000975 const PointerType *PRTy = RTy->getAs<PointerType>();
Ted Kremenek6217b802009-07-29 21:53:49 +0000976 const PointerType *PUsedTy = UsedTy->getAs<PointerType>();
Ted Kremenek25c54572009-07-20 22:58:02 +0000977
978 return PUsedTy && PRTy &&
979 IsReinterpreted(PRTy->getPointeeType(),
Mike Stump1eb44332009-09-09 15:08:12 +0000980 PUsedTy->getPointeeType(), Ctx);
Ted Kremenek25c54572009-07-20 22:58:02 +0000981 }
982
983 return true;
Ted Kremeneka6275a52009-07-15 02:31:43 +0000984}
985
Ted Kremenek0954cde2009-09-24 04:11:44 +0000986const ElementRegion *
987RegionStoreManager::GetElementZeroRegion(const SymbolicRegion *SR, QualType T) {
988 ASTContext &Ctx = getContext();
989 SVal idx = ValMgr.makeZeroArrayIndex();
990 assert(!T.isNull());
991 return MRMgr.getElementRegion(T, idx, SR, Ctx);
992}
993
994
995
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000996SValuator::CastResult
997RegionStoreManager::Retrieve(const GRState *state, Loc L, QualType T) {
Ted Kremenek67f28532009-06-17 22:02:04 +0000998
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000999 assert(!isa<UnknownVal>(L) && "location unknown");
1000 assert(!isa<UndefinedVal>(L) && "location undefined");
1001
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001002 // FIXME: Is this even possible? Shouldn't this be treated as a null
1003 // dereference at a higher level?
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001004 if (isa<loc::ConcreteInt>(L))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001005 return SValuator::CastResult(state, UndefinedVal());
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001006
Ted Kremenek67f28532009-06-17 22:02:04 +00001007 const MemRegion *MR = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xua1718c72009-04-03 07:33:13 +00001008
Zhongxing Xu91844122009-05-20 09:18:48 +00001009 // FIXME: return symbolic value for these cases.
Zhongxing Xua1718c72009-04-03 07:33:13 +00001010 // Example:
1011 // void f(int* p) { int x = *p; }
Zhongxing Xu91844122009-05-20 09:18:48 +00001012 // char* p = alloca();
1013 // read(p);
1014 // c = *p;
Ted Kremenek60fbe8f2009-07-14 20:48:22 +00001015 if (isa<AllocaRegion>(MR))
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001016 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Ted Kremenek0954cde2009-09-24 04:11:44 +00001018 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(MR))
1019 MR = GetElementZeroRegion(SR, T);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Ted Kremenek968f0a62009-08-03 21:41:46 +00001021 if (isa<CodeTextRegion>(MR))
1022 return SValuator::CastResult(state, UnknownVal());
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001024 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1025 // instead of 'Loc', and have the other Loc cases handled at a higher level.
Ted Kremenek67f28532009-06-17 22:02:04 +00001026 const TypedRegion *R = cast<TypedRegion>(MR);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001027 QualType RTy = R->getValueType(getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001028
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001029 // FIXME: We should eventually handle funny addressing. e.g.:
1030 //
1031 // int x = ...;
1032 // int *p = &x;
1033 // char *q = (char*) p;
1034 // char c = *q; // returns the first byte of 'x'.
1035 //
1036 // Such funny addressing will occur due to layering of regions.
1037
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001038#if 0
Ted Kremeneka6275a52009-07-15 02:31:43 +00001039 ASTContext &Ctx = getContext();
1040 if (!T.isNull() && IsReinterpreted(RTy, T, Ctx)) {
Ted Kremenek46537392009-07-16 01:33:37 +00001041 SVal ZeroIdx = ValMgr.makeZeroArrayIndex();
1042 R = MRMgr.getElementRegion(T, ZeroIdx, R, Ctx);
Ted Kremeneka6275a52009-07-15 02:31:43 +00001043 RTy = T;
Ted Kremenek41fb0df2009-07-15 04:23:32 +00001044 assert(Ctx.getCanonicalType(RTy) ==
1045 Ctx.getCanonicalType(R->getValueType(Ctx)));
Mike Stump1eb44332009-09-09 15:08:12 +00001046 }
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001047#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001048
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001049 if (RTy->isStructureType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001050 return SValuator::CastResult(state, RetrieveStruct(state, R));
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Ted Kremenekd4e5a602009-08-06 21:43:54 +00001052 // FIXME: Handle unions.
1053 if (RTy->isUnionType())
1054 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001055
1056 if (RTy->isArrayType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001057 return SValuator::CastResult(state, RetrieveArray(state, R));
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001058
Zhongxing Xu1038f9f2009-03-09 09:15:51 +00001059 // FIXME: handle Vector types.
1060 if (RTy->isVectorType())
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001061 return SValuator::CastResult(state, UnknownVal());
Zhongxing Xu99c20302009-06-28 14:16:39 +00001062
1063 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001064 return SValuator::CastResult(state,
1065 CastRetrievedVal(RetrieveField(state, FR), FR, T));
Zhongxing Xu99c20302009-06-28 14:16:39 +00001066
1067 if (const ElementRegion* ER = dyn_cast<ElementRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001068 return SValuator::CastResult(state,
1069 CastRetrievedVal(RetrieveElement(state, ER), ER, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Ted Kremenek25c54572009-07-20 22:58:02 +00001071 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001072 return SValuator::CastResult(state,
1073 CastRetrievedVal(RetrieveObjCIvar(state, IVR), IVR, T));
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Ted Kremenek9031dd72009-07-21 00:12:07 +00001075 if (const VarRegion *VR = dyn_cast<VarRegion>(R))
Zhongxing Xu652be342009-11-16 04:49:44 +00001076 return SValuator::CastResult(state,
1077 CastRetrievedVal(RetrieveVar(state, VR), VR, T));
Ted Kremenek25c54572009-07-20 22:58:02 +00001078
Ted Kremenek451ac092009-08-06 04:50:20 +00001079 RegionBindings B = GetRegionBindings(state->getStore());
1080 RegionBindings::data_type* V = B.lookup(R);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001081
1082 // Check if the region has a binding.
1083 if (V)
Zhongxing Xu13d50172009-10-11 08:08:02 +00001084 if (SVal const *SV = V->getValue())
1085 return SValuator::CastResult(state, *SV);
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001086
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001087 // The location does not have a bound value. This means that it has
1088 // the value it had upon its creation and/or entry to the analyzed
1089 // function/method. These are either symbolic values or 'undefined'.
1090
Ted Kremenek356e9d62009-07-22 04:35:42 +00001091#if HEAP_UNDEFINED
Ted Kremenekbb7c96f2009-06-23 18:17:08 +00001092 if (R->hasHeapOrStackStorage()) {
Ted Kremenek356e9d62009-07-22 04:35:42 +00001093#else
1094 if (R->hasStackStorage()) {
1095#endif
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001096 // All stack variables are considered to have undefined values
1097 // upon creation. All heap allocated blocks are considered to
1098 // have undefined values as well unless they are explicitly bound
1099 // to specific values.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001100 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek869fb4a2008-12-24 07:46:32 +00001101 }
1102
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001103 // All other values are symbolic.
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001104 return SValuator::CastResult(state,
1105 ValMgr.getRegionValueSymbolValOrUnknown(R, RTy));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001106}
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001108std::pair<const GRState*, const MemRegion*>
Ted Kremenek451ac092009-08-06 04:50:20 +00001109RegionStoreManager::GetLazyBinding(RegionBindings B, const MemRegion *R) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001110 if (Optional<SVal> OV = getDirectBinding(B, R))
1111 if (const nonloc::LazyCompoundVal *V =
1112 dyn_cast<nonloc::LazyCompoundVal>(OV.getPointer()))
1113 return std::make_pair(V->getState(), V->getRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001115 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1116 const std::pair<const GRState *, const MemRegion *> &X =
1117 GetLazyBinding(B, ER->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001119 if (X.first)
1120 return std::make_pair(X.first,
1121 MRMgr.getElementRegionWithSuper(ER, X.second));
Mike Stump1eb44332009-09-09 15:08:12 +00001122 }
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001123 else if (const FieldRegion *FR = dyn_cast<FieldRegion>(R)) {
1124 const std::pair<const GRState *, const MemRegion *> &X =
1125 GetLazyBinding(B, FR->getSuperRegion());
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001127 if (X.first)
1128 return std::make_pair(X.first,
1129 MRMgr.getFieldRegionWithSuper(FR, X.second));
1130 }
1131
1132 return std::make_pair((const GRState*) 0, (const MemRegion *) 0);
1133}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001134
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001135SVal RegionStoreManager::RetrieveElement(const GRState* state,
1136 const ElementRegion* R) {
1137 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001138 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001139 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001140 return *V;
1141
Ted Kremenek921109a2009-07-01 23:19:52 +00001142 const MemRegion* superR = R->getSuperRegion();
1143
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001144 // Check if the region is an element region of a string literal.
Ted Kremenek921109a2009-07-01 23:19:52 +00001145 if (const StringRegion *StrR=dyn_cast<StringRegion>(superR)) {
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001146 // FIXME: Handle loads from strings where the literal is treated as
1147 // an integer, e.g., *((unsigned int*)"hello")
1148 ASTContext &Ctx = getContext();
Douglas Gregor89c49f02009-11-09 22:08:55 +00001149 QualType T = Ctx.getAsArrayType(StrR->getValueType(Ctx))->getElementType();
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001150 if (T != Ctx.getCanonicalType(R->getElementType()))
1151 return UnknownVal();
1152
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001153 const StringLiteral *Str = StrR->getStringLiteral();
1154 SVal Idx = R->getIndex();
1155 if (nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Idx)) {
1156 int64_t i = CI->getValue().getSExtValue();
Mike Stump1eb44332009-09-09 15:08:12 +00001157 int64_t byteLength = Str->getByteLength();
Ted Kremenek0667db32009-09-05 17:59:01 +00001158 if (i > byteLength) {
1159 // Buffer overflow checking in GRExprEngine should handle this case,
1160 // but we shouldn't rely on it to not overflow here if that checking
1161 // is disabled.
1162 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001163 }
Ted Kremenek0667db32009-09-05 17:59:01 +00001164 char c = (i == byteLength) ? '\0' : Str->getStrData()[i];
Ted Kremenek95efe0f2009-09-29 16:36:48 +00001165 return ValMgr.makeIntVal(c, T);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001166 }
1167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001169 // Check if the immediate super region has a direct binding.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001170 if (Optional<SVal> V = getDirectBinding(B, superR)) {
Ted Kremeneka6275a52009-07-15 02:31:43 +00001171 if (SymbolRef parentSym = V->getAsSymbol())
1172 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Ted Kremenek356e9d62009-07-22 04:35:42 +00001173
1174 if (V->isUnknownOrUndef())
1175 return *V;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001176
1177 // Handle LazyCompoundVals for the immediate super region. Other cases
1178 // are handled in 'RetrieveFieldOrElementCommon'.
Mike Stump1eb44332009-09-09 15:08:12 +00001179 if (const nonloc::LazyCompoundVal *LCV =
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001180 dyn_cast<nonloc::LazyCompoundVal>(V)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001181
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001182 R = MRMgr.getElementRegionWithSuper(R, LCV->getRegion());
1183 return RetrieveElement(LCV->getState(), R);
1184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Ted Kremeneka6275a52009-07-15 02:31:43 +00001186 // Other cases: give up.
Zhongxing Xu8834af32009-07-03 06:11:41 +00001187 return UnknownVal();
Zhongxing Xu7abe0192009-06-30 12:32:59 +00001188 }
Zhongxing Xu13d50172009-10-11 08:08:02 +00001189
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001190 return RetrieveFieldOrElementCommon(state, R, R->getElementType(), superR);
Zhongxing Xuc00346f2009-06-25 05:29:39 +00001191}
1192
Mike Stump1eb44332009-09-09 15:08:12 +00001193SVal RegionStoreManager::RetrieveField(const GRState* state,
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001194 const FieldRegion* R) {
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001195
1196 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001197 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001198 if (Optional<SVal> V = getDirectBinding(B, R))
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001199 return *V;
1200
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001201 QualType Ty = R->getValueType(getContext());
1202 return RetrieveFieldOrElementCommon(state, R, Ty, R->getSuperRegion());
1203}
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001205SVal RegionStoreManager::RetrieveFieldOrElementCommon(const GRState *state,
1206 const TypedRegion *R,
1207 QualType Ty,
1208 const MemRegion *superR) {
1209
Mike Stump1eb44332009-09-09 15:08:12 +00001210 // At this point we have already checked in either RetrieveElement or
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001211 // RetrieveField if 'R' has a direct binding.
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001213 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001215 while (superR) {
Zhongxing Xu13d50172009-10-11 08:08:02 +00001216 if (const Optional<SVal> &D = getDefaultBinding(B, superR)) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001217 if (SymbolRef parentSym = D->getAsSymbol())
1218 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001220 if (D->isZeroConstant())
1221 return ValMgr.makeZeroVal(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001223 if (D->isUnknown())
1224 return *D;
Mike Stump1eb44332009-09-09 15:08:12 +00001225
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001226 assert(0 && "Unknown default value");
1227 }
Mike Stump1eb44332009-09-09 15:08:12 +00001228
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001229 // If our super region is a field or element itself, walk up the region
1230 // hierarchy to see if there is a default value installed in an ancestor.
1231 if (isa<FieldRegion>(superR) || isa<ElementRegion>(superR)) {
1232 superR = cast<SubRegion>(superR)->getSuperRegion();
1233 continue;
1234 }
Mike Stump1eb44332009-09-09 15:08:12 +00001235
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001236 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001237 }
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001239 // Lazy binding?
1240 const GRState *lazyBindingState = NULL;
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001241 const MemRegion *lazyBindingRegion = NULL;
1242 llvm::tie(lazyBindingState, lazyBindingRegion) = GetLazyBinding(B, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001244 if (lazyBindingState) {
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001245 assert(lazyBindingRegion && "Lazy-binding region not set");
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001247 if (isa<ElementRegion>(R))
1248 return RetrieveElement(lazyBindingState,
1249 cast<ElementRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001251 return RetrieveField(lazyBindingState,
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001252 cast<FieldRegion>(lazyBindingRegion));
Mike Stump1eb44332009-09-09 15:08:12 +00001253 }
1254
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001255 if (R->hasStackStorage() && !R->hasParametersStorage()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001257 if (isa<ElementRegion>(R)) {
1258 // Currently we don't reason specially about Clang-style vectors. Check
1259 // if superR is a vector and if so return Unknown.
1260 if (const TypedRegion *typedSuperR = dyn_cast<TypedRegion>(superR)) {
1261 if (typedSuperR->getValueType(getContext())->isVectorType())
1262 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +00001263 }
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001264 }
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001266 return UndefinedVal();
Ted Kremenek566a6fa2009-08-06 22:33:36 +00001267 }
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Ted Kremenekbb2b4332009-07-02 22:16:42 +00001269 // All other values are symbolic.
1270 return ValMgr.getRegionValueSymbolValOrUnknown(R, Ty);
Zhongxing Xu490b0f02009-06-25 04:50:44 +00001271}
Mike Stump1eb44332009-09-09 15:08:12 +00001272
1273SVal RegionStoreManager::RetrieveObjCIvar(const GRState* state,
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001274 const ObjCIvarRegion* R) {
1275
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001276 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001277 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001278
Zhongxing Xu13d50172009-10-11 08:08:02 +00001279 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001280 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001282 const MemRegion *superR = R->getSuperRegion();
1283
Ted Kremenekab22ee92009-10-20 01:20:57 +00001284 // Check if the super region has a default binding.
1285 if (Optional<SVal> V = getDefaultBinding(B, superR)) {
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001286 if (SymbolRef parentSym = V->getAsSymbol())
1287 return ValMgr.getDerivedRegionValueSymbolVal(parentSym, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001289 // Other cases: give up.
1290 return UnknownVal();
1291 }
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Ted Kremenek25c54572009-07-20 22:58:02 +00001293 return RetrieveLazySymbol(state, R);
1294}
1295
Ted Kremenek9031dd72009-07-21 00:12:07 +00001296SVal RegionStoreManager::RetrieveVar(const GRState *state,
1297 const VarRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Ted Kremenek9031dd72009-07-21 00:12:07 +00001299 // Check if the region has a binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001300 RegionBindings B = GetRegionBindings(state->getStore());
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Zhongxing Xu13d50172009-10-11 08:08:02 +00001302 if (Optional<SVal> V = getDirectBinding(B, R))
Ted Kremenek9031dd72009-07-21 00:12:07 +00001303 return *V;
Mike Stump1eb44332009-09-09 15:08:12 +00001304
Ted Kremenek9031dd72009-07-21 00:12:07 +00001305 // Lazily derive a value for the VarRegion.
1306 const VarDecl *VD = R->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Ted Kremenek9031dd72009-07-21 00:12:07 +00001308 if (R->hasGlobalsOrParametersStorage())
1309 return ValMgr.getRegionValueSymbolValOrUnknown(R, VD->getType());
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Ted Kremenek9031dd72009-07-21 00:12:07 +00001311 return UndefinedVal();
1312}
1313
Mike Stump1eb44332009-09-09 15:08:12 +00001314SVal RegionStoreManager::RetrieveLazySymbol(const GRState *state,
Ted Kremenek25c54572009-07-20 22:58:02 +00001315 const TypedRegion *R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001316
Ted Kremenek25c54572009-07-20 22:58:02 +00001317 QualType valTy = R->getValueType(getContext());
Ted Kremenek356e9d62009-07-22 04:35:42 +00001318
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001319 // All other values are symbolic.
Ted Kremenek25c54572009-07-20 22:58:02 +00001320 return ValMgr.getRegionValueSymbolValOrUnknown(R, valTy);
Ted Kremenek5bd2fe32009-07-15 06:09:28 +00001321}
1322
Mike Stump1eb44332009-09-09 15:08:12 +00001323SVal RegionStoreManager::RetrieveStruct(const GRState *state,
1324 const TypedRegion* R) {
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001325 QualType T = R->getValueType(getContext());
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001326 assert(T->isStructureType());
1327
Zhongxing Xub7507d12009-06-11 07:27:30 +00001328 const RecordType* RT = T->getAsStructureType();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001329 RecordDecl* RD = RT->getDecl();
1330 assert(RD->isDefinition());
Mike Stump1aeb2472009-08-06 12:56:50 +00001331 (void)RD;
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001332#if USE_EXPLICIT_COMPOUND
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001333 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
1334
Ted Kremenek67f28532009-06-17 22:02:04 +00001335 // FIXME: We shouldn't use a std::vector. If RecordDecl doesn't have a
1336 // reverse iterator, we should implement one.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001337 std::vector<FieldDecl *> Fields(RD->field_begin(), RD->field_end());
Douglas Gregor44b43212008-12-11 16:49:14 +00001338
Douglas Gregore267ff32008-12-11 20:41:00 +00001339 for (std::vector<FieldDecl *>::reverse_iterator Field = Fields.rbegin(),
1340 FieldEnd = Fields.rend();
1341 Field != FieldEnd; ++Field) {
1342 FieldRegion* FR = MRMgr.getFieldRegion(*Field, R);
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001343 QualType FTy = (*Field)->getType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001344 SVal FieldValue = Retrieve(state, loc::MemRegionVal(FR), FTy).getSVal();
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001345 StructVal = getBasicVals().consVals(FieldValue, StructVal);
1346 }
1347
Zhongxing Xud91ee272009-06-23 09:02:15 +00001348 return ValMgr.makeCompoundVal(T, StructVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001349#else
1350 return ValMgr.makeLazyCompoundVal(state, R);
1351#endif
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +00001352}
1353
Ted Kremenek67f28532009-06-17 22:02:04 +00001354SVal RegionStoreManager::RetrieveArray(const GRState *state,
1355 const TypedRegion * R) {
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001356#if USE_EXPLICIT_COMPOUND
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001357 QualType T = R->getValueType(getContext());
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001358 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
1359
1360 llvm::ImmutableList<SVal> ArrayVal = getBasicVals().getEmptySValList();
Ted Kremenek46537392009-07-16 01:33:37 +00001361 uint64_t size = CAT->getSize().getZExtValue();
1362 for (uint64_t i = 0; i < size; ++i) {
1363 SVal Idx = ValMgr.makeArrayIndex(i);
Zhongxing Xu143b2fc2009-06-16 09:55:50 +00001364 ElementRegion* ER = MRMgr.getElementRegion(CAT->getElementType(), Idx, R,
Mike Stump1eb44332009-09-09 15:08:12 +00001365 getContext());
Ted Kremenekf936f452009-05-04 06:18:28 +00001366 QualType ETy = ER->getElementType();
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001367 SVal ElementVal = Retrieve(state, loc::MemRegionVal(ER), ETy).getSVal();
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001368 ArrayVal = getBasicVals().consVals(ElementVal, ArrayVal);
1369 }
1370
Zhongxing Xud91ee272009-06-23 09:02:15 +00001371 return ValMgr.makeCompoundVal(T, ArrayVal);
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001372#else
1373 assert(isa<ConstantArrayType>(R->getValueType(getContext())));
1374 return ValMgr.makeLazyCompoundVal(state, R);
1375#endif
Zhongxing Xu3e001f32009-05-03 00:27:40 +00001376}
1377
Ted Kremenek9af46f52009-06-16 22:36:44 +00001378//===----------------------------------------------------------------------===//
1379// Binding values to regions.
1380//===----------------------------------------------------------------------===//
Zhongxing Xu17892752008-10-08 02:50:44 +00001381
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001382Store RegionStoreManager::Remove(Store store, Loc L) {
Ted Kremenek0964a062009-01-21 06:57:53 +00001383 const MemRegion* R = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Ted Kremenek0964a062009-01-21 06:57:53 +00001385 if (isa<loc::MemRegionVal>(L))
1386 R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Ted Kremenek0964a062009-01-21 06:57:53 +00001388 if (R) {
Mike Stump1eb44332009-09-09 15:08:12 +00001389 RegionBindings B = GetRegionBindings(store);
Ted Kremenek0964a062009-01-21 06:57:53 +00001390 return RBFactory.Remove(B, R).getRoot();
1391 }
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Ted Kremenek0964a062009-01-21 06:57:53 +00001393 return store;
Zhongxing Xu9c9ca082008-12-16 02:36:30 +00001394}
1395
Ted Kremenek67f28532009-06-17 22:02:04 +00001396const GRState *RegionStoreManager::Bind(const GRState *state, Loc L, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +00001397 if (isa<loc::ConcreteInt>(L))
1398 return state;
1399
Ted Kremenek9af46f52009-06-16 22:36:44 +00001400 // If we get here, the location should be a region.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001401 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Ted Kremenek9af46f52009-06-16 22:36:44 +00001403 // Check if the region is a struct region.
1404 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
1405 if (TR->getValueType(getContext())->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001406 return BindStruct(state, TR, V);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001408 // Special case: the current region represents a cast and it and the super
1409 // region both have pointer types or intptr_t types. If so, perform the
1410 // bind to the super region.
1411 // This is needed to support OSAtomicCompareAndSwap and friends or other
Mike Stump1eb44332009-09-09 15:08:12 +00001412 // loads that treat integers as pointers and vis versa.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001413 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
1414 if (ER->getIndex().isZeroConstant()) {
1415 if (const TypedRegion *superR =
1416 dyn_cast<TypedRegion>(ER->getSuperRegion())) {
1417 ASTContext &Ctx = getContext();
1418 QualType superTy = superR->getValueType(Ctx);
1419 QualType erTy = ER->getValueType(Ctx);
Mike Stump1eb44332009-09-09 15:08:12 +00001420
1421 if (IsAnyPointerOrIntptr(superTy, Ctx) &&
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001422 IsAnyPointerOrIntptr(erTy, Ctx)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001423 SValuator::CastResult cr =
1424 ValMgr.getSValuator().EvalCast(V, state, superTy, erTy);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001425 return Bind(cr.getState(), loc::MemRegionVal(superR), cr.getSVal());
1426 }
Ted Kremenek69181a82009-09-21 22:58:52 +00001427 // For now, just invalidate the fields of the struct/union/class.
1428 // FIXME: Precisely handle the fields of the record.
1429 if (superTy->isRecordType())
Ted Kremenek473e1672009-10-16 00:30:49 +00001430 return InvalidateRegion(state, superR, NULL, 0, NULL);
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001431 }
1432 }
1433 }
Ted Kremenek0954cde2009-09-24 04:11:44 +00001434 else if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
1435 // Binding directly to a symbolic region should be treated as binding
1436 // to element 0.
1437 QualType T = SR->getSymbol()->getType(getContext());
Ted Kremenek35dcad82009-09-24 06:24:32 +00001438 T = T->getAs<PointerType>()->getPointeeType();
Ted Kremenek0954cde2009-09-24 04:11:44 +00001439 R = GetElementZeroRegion(SR, T);
1440 }
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001442 // Perform the binding.
Ted Kremenek451ac092009-08-06 04:50:20 +00001443 RegionBindings B = GetRegionBindings(state->getStore());
Zhongxing Xu13d50172009-10-11 08:08:02 +00001444 return state->makeWithStore(
1445 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremenek9af46f52009-06-16 22:36:44 +00001446}
1447
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001448const GRState *RegionStoreManager::BindDecl(const GRState *ST,
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001449 const VarRegion *VR,
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001450 SVal InitVal) {
Zhongxing Xua4f28ff2008-11-13 08:41:36 +00001451
Ted Kremenekf6f56d42009-11-04 00:09:15 +00001452 QualType T = VR->getDecl()->getType();
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +00001453
Ted Kremenek0964a062009-01-21 06:57:53 +00001454 if (T->isArrayType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001455 return BindArray(ST, VR, InitVal);
Ted Kremenek0964a062009-01-21 06:57:53 +00001456 if (T->isStructureType())
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001457 return BindStruct(ST, VR, InitVal);
Zhongxing Xud463d442008-11-02 12:13:30 +00001458
Ted Kremenekd17da2b2009-08-21 22:28:32 +00001459 return Bind(ST, ValMgr.makeLoc(VR), InitVal);
Zhongxing Xu17892752008-10-08 02:50:44 +00001460}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +00001461
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001462// FIXME: this method should be merged into Bind().
Ted Kremenek67f28532009-06-17 22:02:04 +00001463const GRState *
1464RegionStoreManager::BindCompoundLiteral(const GRState *state,
1465 const CompoundLiteralExpr* CL,
1466 SVal V) {
Ted Kremenekb48ad642009-12-04 00:26:31 +00001467 return Bind(state, loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL)), V);
Zhongxing Xuf22679e2008-11-07 10:38:33 +00001468}
1469
Ted Kremenek027e2662009-11-19 20:20:24 +00001470const GRState *RegionStoreManager::setImplicitDefaultValue(const GRState *state,
1471 const MemRegion *R,
1472 QualType T) {
1473 Store store = state->getStore();
1474 RegionBindings B = GetRegionBindings(store);
1475 SVal V;
1476
1477 if (Loc::IsLocType(T))
1478 V = ValMgr.makeNull();
1479 else if (T->isIntegerType())
1480 V = ValMgr.makeZeroVal(T);
1481 else if (T->isStructureType() || T->isArrayType()) {
1482 // Set the default value to a zero constant when it is a structure
1483 // or array. The type doesn't really matter.
1484 V = ValMgr.makeZeroVal(ValMgr.getContext().IntTy);
1485 }
1486 else {
1487 return state;
1488 }
1489
1490 B = RBFactory.Add(B, R, BindingVal(V, BindingVal::Default));
1491 return state->makeWithStore(B.getRoot());
1492}
1493
Ted Kremenek67f28532009-06-17 22:02:04 +00001494const GRState *RegionStoreManager::BindArray(const GRState *state,
Ted Kremenek46537392009-07-16 01:33:37 +00001495 const TypedRegion* R,
Ted Kremenek67f28532009-06-17 22:02:04 +00001496 SVal Init) {
1497
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001498 QualType T = R->getValueType(getContext());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001499 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001500 QualType ElementTy = CAT->getElementType();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001501
Ted Kremenek46537392009-07-16 01:33:37 +00001502 uint64_t size = CAT->getSize().getZExtValue();
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001503
1504 // Check if the init expr is a StringLiteral.
1505 if (isa<loc::MemRegionVal>(Init)) {
1506 const MemRegion* InitR = cast<loc::MemRegionVal>(Init).getRegion();
1507 const StringLiteral* S = cast<StringRegion>(InitR)->getStringLiteral();
1508 const char* str = S->getStrData();
1509 unsigned len = S->getByteLength();
1510 unsigned j = 0;
1511
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001512 // Copy bytes from the string literal into the target array. Trailing bytes
1513 // in the array that are not covered by the string literal are initialized
1514 // to zero.
Ted Kremenek46537392009-07-16 01:33:37 +00001515 for (uint64_t i = 0; i < size; ++i, ++j) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001516 if (j >= len)
1517 break;
1518
Ted Kremenek46537392009-07-16 01:33:37 +00001519 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001520 const ElementRegion* ER = MRMgr.getElementRegion(ElementTy, Idx, R,
1521 getContext());
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001522
Zhongxing Xud91ee272009-06-23 09:02:15 +00001523 SVal V = ValMgr.makeIntVal(str[j], sizeof(char)*8, true);
Ted Kremenek67f28532009-06-17 22:02:04 +00001524 state = Bind(state, loc::MemRegionVal(ER), V);
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001525 }
1526
Ted Kremenek67f28532009-06-17 22:02:04 +00001527 return state;
Zhongxing Xu6987c7b2008-11-30 05:49:49 +00001528 }
1529
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001530 // Handle lazy compound values.
1531 if (nonloc::LazyCompoundVal *LCV = dyn_cast<nonloc::LazyCompoundVal>(&Init))
1532 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001533
1534 // Remaining case: explicit compound values.
Ted Kremenek027e2662009-11-19 20:20:24 +00001535
1536 if (Init.isUnknown())
1537 return setImplicitDefaultValue(state, R, ElementTy);
1538
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001539 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001540 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Ted Kremenek46537392009-07-16 01:33:37 +00001541 uint64_t i = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Ted Kremenek46537392009-07-16 01:33:37 +00001543 for (; i < size; ++i, ++VI) {
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001544 // The init list might be shorter than the array length.
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001545 if (VI == VE)
1546 break;
1547
Ted Kremenek46537392009-07-16 01:33:37 +00001548 SVal Idx = ValMgr.makeArrayIndex(i);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001549 const ElementRegion *ER = MRMgr.getElementRegion(ElementTy, Idx, R, getContext());
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001550
1551 if (CAT->getElementType()->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001552 state = BindStruct(state, ER, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001553 else
Ted Kremenekcf549592009-09-22 21:19:14 +00001554 // FIXME: Do we need special handling of nested arrays?
Zhongxing Xud91ee272009-06-23 09:02:15 +00001555 state = Bind(state, ValMgr.makeLoc(ER), *VI);
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001556 }
1557
Ted Kremenek027e2662009-11-19 20:20:24 +00001558 // If the init list is shorter than the array length, set the
1559 // array default value.
1560 if (i < size)
1561 state = setImplicitDefaultValue(state, R, ElementTy);
Zhongxing Xu087d6c22009-06-23 05:23:38 +00001562
Ted Kremenek67f28532009-06-17 22:02:04 +00001563 return state;
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +00001564}
1565
Ted Kremenek67f28532009-06-17 22:02:04 +00001566const GRState *
1567RegionStoreManager::BindStruct(const GRState *state, const TypedRegion* R,
1568 SVal V) {
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Ted Kremenek67f28532009-06-17 22:02:04 +00001570 if (!Features.supportsFields())
1571 return state;
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Zhongxing Xua82d8aa2009-05-09 03:57:34 +00001573 QualType T = R->getValueType(getContext());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001574 assert(T->isStructureType());
1575
Ted Kremenek6217b802009-07-29 21:53:49 +00001576 const RecordType* RT = T->getAs<RecordType>();
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001577 RecordDecl* RD = RT->getDecl();
Zhongxing Xuc45a8252009-03-11 09:07:35 +00001578
1579 if (!RD->isDefinition())
Ted Kremenek67f28532009-06-17 22:02:04 +00001580 return state;
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001581
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001582 // Handle lazy compound values.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001583 if (const nonloc::LazyCompoundVal *LCV=dyn_cast<nonloc::LazyCompoundVal>(&V))
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001584 return CopyLazyBindings(*LCV, state, R);
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Ted Kremenek67f28532009-06-17 22:02:04 +00001586 // We may get non-CompoundVal accidentally due to imprecise cast logic.
1587 // Ignore them and kill the field values.
1588 if (V.isUnknown() || !isa<nonloc::CompoundVal>(V))
Zhongxing Xu13d50172009-10-11 08:08:02 +00001589 return state->makeWithStore(KillStruct(state->getStore(), R));
Zhongxing Xu3f6978a2009-06-11 09:11:27 +00001590
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001591 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001592 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001593
1594 RecordDecl::field_iterator FI, FE;
1595
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001596 for (FI = RD->field_begin(), FE = RD->field_end(); FI != FE; ++FI, ++VI) {
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001597
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001598 if (VI == VE)
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001599 break;
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001600
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001601 QualType FTy = (*FI)->getType();
Ted Kremenekcf549592009-09-22 21:19:14 +00001602 const FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
Zhongxing Xuaf0a8442008-10-31 10:53:01 +00001603
Ted Kremenekcf549592009-09-22 21:19:14 +00001604 if (FTy->isArrayType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001605 state = BindArray(state, FR, *VI);
Zhongxing Xu4193eca2008-12-20 06:32:12 +00001606 else if (FTy->isStructureType())
Ted Kremenek67f28532009-06-17 22:02:04 +00001607 state = BindStruct(state, FR, *VI);
Ted Kremenekcf549592009-09-22 21:19:14 +00001608 else
1609 state = Bind(state, ValMgr.makeLoc(FR), *VI);
Zhongxing Xua82512a2008-10-24 08:42:28 +00001610 }
1611
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001612 // There may be fewer values in the initialize list than the fields of struct.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001613 if (FI != FE) {
1614 Store store = state->getStore();
1615 RegionBindings B = GetRegionBindings(store);
1616 B = RBFactory.Add(B, R,
1617 BindingVal(ValMgr.makeIntVal(0, false), BindingVal::Default));
1618 state = state->makeWithStore(B.getRoot());
1619 }
Zhongxing Xudbdf2192009-06-23 05:43:16 +00001620
Ted Kremenek67f28532009-06-17 22:02:04 +00001621 return state;
Zhongxing Xuc3a05992008-11-19 11:06:24 +00001622}
1623
Zhongxing Xu13d50172009-10-11 08:08:02 +00001624Store RegionStoreManager::KillStruct(Store store, const TypedRegion* R) {
1625 RegionBindings B = GetRegionBindings(store);
1626 llvm::OwningPtr<RegionStoreSubRegionMap>
1627 SubRegions(getRegionStoreSubRegionMap(store));
1628 RemoveSubRegionBindings(B, R, *SubRegions);
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001629
Zhongxing Xue4df9c42009-06-25 05:52:16 +00001630 // Set the default value of the struct region to "unknown".
Zhongxing Xu13d50172009-10-11 08:08:02 +00001631 B = RBFactory.Add(B, R, BindingVal(UnknownVal(), BindingVal::Default));
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001632
Zhongxing Xu13d50172009-10-11 08:08:02 +00001633 return B.getRoot();
Zhongxing Xu5834ed62009-01-13 01:49:57 +00001634}
1635
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001636const GRState*
1637RegionStoreManager::CopyLazyBindings(nonloc::LazyCompoundVal V,
1638 const GRState *state,
1639 const TypedRegion *R) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001640
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001641 // Nuke the old bindings stemming from R.
Ted Kremenek451ac092009-08-06 04:50:20 +00001642 RegionBindings B = GetRegionBindings(state->getStore());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001643
Mike Stump1eb44332009-09-09 15:08:12 +00001644 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001645 SubRegions(getRegionStoreSubRegionMap(state->getStore()));
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001646
Mike Stump1eb44332009-09-09 15:08:12 +00001647 // B and DVM are updated after the call to RemoveSubRegionBindings.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001648 RemoveSubRegionBindings(B, R, *SubRegions.get());
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001650 // Now copy the bindings. This amounts to just binding 'V' to 'R'. This
1651 // results in a zero-copy algorithm.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001652 return state->makeWithStore(
1653 RBFactory.Add(B, R, BindingVal(V, BindingVal::Direct)).getRoot());
Ted Kremeneka5e81f12009-08-06 01:20:57 +00001654}
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Ted Kremenek9af46f52009-06-16 22:36:44 +00001656//===----------------------------------------------------------------------===//
1657// State pruning.
1658//===----------------------------------------------------------------------===//
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001659
Mike Stump1eb44332009-09-09 15:08:12 +00001660void RegionStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001661 SymbolReaper& SymReaper,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001662 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
Mike Stump1eb44332009-09-09 15:08:12 +00001663{
Ted Kremenek781115c2009-10-17 17:45:11 +00001664 typedef std::pair<const GRState*, const MemRegion *> RBDNode;
1665
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001666 Store store = state.getStore();
Ted Kremenek451ac092009-08-06 04:50:20 +00001667 RegionBindings B = GetRegionBindings(store);
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Ted Kremenek9af46f52009-06-16 22:36:44 +00001669 // The backmap from regions to subregions.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001670 llvm::OwningPtr<RegionStoreSubRegionMap>
Zhongxing Xu13d50172009-10-11 08:08:02 +00001671 SubRegions(getRegionStoreSubRegionMap(store));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001672
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001673 // Do a pass over the regions in the store. For VarRegions we check if
1674 // the variable is still live and if so add it to the list of live roots.
1675 // For other regions we populate our region backmap.
Ted Kremenek9af46f52009-06-16 22:36:44 +00001676 llvm::SmallVector<const MemRegion*, 10> IntermediateRoots;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001677
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001678 // Scan the direct bindings for "intermediate" roots.
Ted Kremenek451ac092009-08-06 04:50:20 +00001679 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001680 const MemRegion *R = I.getKey();
1681 IntermediateRoots.push_back(R);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001682 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001683
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001684 // Process the "intermediate" roots to find if they are referenced by
Mike Stump1eb44332009-09-09 15:08:12 +00001685 // real roots.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001686 llvm::SmallVector<RBDNode, 10> WorkList;
Ted Kremenek01756192009-10-29 05:14:17 +00001687 llvm::SmallVector<RBDNode, 10> Postponed;
1688
Zhongxing Xu6800b332009-10-18 04:15:47 +00001689 llvm::DenseSet<const MemRegion*> IntermediateVisited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001690
Ted Kremenek9af46f52009-06-16 22:36:44 +00001691 while (!IntermediateRoots.empty()) {
1692 const MemRegion* R = IntermediateRoots.back();
1693 IntermediateRoots.pop_back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001694
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001695 if (IntermediateVisited.count(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001696 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001697 IntermediateVisited.insert(R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001698
Ted Kremenek9af46f52009-06-16 22:36:44 +00001699 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
Ted Kremenekedeb5b62009-12-04 20:32:20 +00001700 if (SymReaper.isLive(Loc, VR))
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001701 WorkList.push_back(std::make_pair(&state, VR));
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001702 continue;
1703 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001704
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001705 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
Ted Kremenek01756192009-10-29 05:14:17 +00001706 llvm::SmallVectorImpl<RBDNode> &Q =
1707 SymReaper.isLive(SR->getSymbol()) ? WorkList : Postponed;
1708
1709 Q.push_back(std::make_pair(&state, SR));
1710
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001711 continue;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001712 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001713
1714 // Add the super region for R to the worklist if it is a subregion.
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001715 if (const SubRegion* superR =
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001716 dyn_cast<SubRegion>(cast<SubRegion>(R)->getSuperRegion()))
Ted Kremenek19e1f0b2009-08-01 06:17:29 +00001717 IntermediateRoots.push_back(superR);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001718 }
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001720 // Enqueue the RegionRoots onto WorkList.
1721 for (llvm::SmallVectorImpl<const MemRegion*>::iterator I=RegionRoots.begin(),
1722 E=RegionRoots.end(); I!=E; ++I) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001723 WorkList.push_back(std::make_pair(&state, *I));
Mike Stump1eb44332009-09-09 15:08:12 +00001724 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001725 RegionRoots.clear();
1726
Zhongxing Xu6800b332009-10-18 04:15:47 +00001727 llvm::DenseSet<RBDNode> Visited;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001728
Ted Kremenek01756192009-10-29 05:14:17 +00001729tryAgain:
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001730 while (!WorkList.empty()) {
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001731 RBDNode N = WorkList.back();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001732 WorkList.pop_back();
1733
1734 // Have we visited this node before?
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001735 if (Visited.count(N))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001736 continue;
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001737 Visited.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001739 const MemRegion *R = N.second;
1740 const GRState *state_N = N.first;
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001741
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001742 // Enqueue subregions.
1743 RegionStoreSubRegionMap *M;
1744
1745 if (&state == state_N)
1746 M = SubRegions.get();
1747 else {
1748 RegionStoreSubRegionMap *& SM = SC[state_N];
1749 if (!SM)
1750 SM = getRegionStoreSubRegionMap(state_N->getStore());
1751 M = SM;
1752 }
1753
1754 RegionStoreSubRegionMap::iterator I, E;
1755 for (llvm::tie(I, E) = M->begin_end(R); I != E; ++I)
1756 WorkList.push_back(std::make_pair(state_N, *I));
1757
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001758 // Enqueue the super region.
1759 if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
1760 const MemRegion *superR = SR->getSuperRegion();
1761 if (!isa<MemSpaceRegion>(superR)) {
1762 // If 'R' is a field or an element, we want to keep the bindings
1763 // for the other fields and elements around. The reason is that
Zhongxing Xu13d50172009-10-11 08:08:02 +00001764 // pointer arithmetic can get us to the other fields or elements.
Zhongxing Xu8801beb2009-10-17 07:32:08 +00001765 assert(isa<FieldRegion>(R) || isa<ElementRegion>(R)
1766 || isa<ObjCIvarRegion>(R));
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001767 WorkList.push_back(std::make_pair(state_N, superR));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001768 }
1769 }
1770
1771 // Mark the symbol for any live SymbolicRegion as "live". This means we
1772 // should continue to track that symbol.
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001773 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001774 SymReaper.markLive(SymR->getSymbol());
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001775
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001776 // For BlockDataRegions, enqueue the VarRegions for variables marked
1777 // with __block (passed-by-reference).
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001778 // via BlockDeclRefExprs.
1779 if (const BlockDataRegion *BD = dyn_cast<BlockDataRegion>(R)) {
1780 for (BlockDataRegion::referenced_vars_iterator
1781 RI = BD->referenced_vars_begin(), RE = BD->referenced_vars_end();
Ted Kremeneka7a8dfd2009-12-03 17:48:05 +00001782 RI != RE; ++RI) {
1783 if ((*RI)->getDecl()->getAttr<BlocksAttr>())
1784 WorkList.push_back(std::make_pair(state_N, *RI));
1785 }
Ted Kremeneka6d73af2009-11-26 02:35:42 +00001786 // No possible data bindings on a BlockDataRegion. Continue to the
1787 // next region in the worklist.
1788 continue;
1789 }
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001790
1791 Store store_N = state_N->getStore();
1792 RegionBindings B_N = GetRegionBindings(store_N);
1793
1794 // Get the data binding for R (if any).
Zhongxing Xu13d50172009-10-11 08:08:02 +00001795 Optional<SVal> V = getBinding(B_N, R);
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001796
Zhongxing Xu13d50172009-10-11 08:08:02 +00001797 if (V) {
1798 // Check for lazy bindings.
1799 if (const nonloc::LazyCompoundVal *LCV =
1800 dyn_cast<nonloc::LazyCompoundVal>(V.getPointer())) {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001801
Zhongxing Xu13d50172009-10-11 08:08:02 +00001802 const LazyCompoundValData *D = LCV->getCVData();
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001803 WorkList.push_back(std::make_pair(D->getState(), D->getRegion()));
Zhongxing Xu13d50172009-10-11 08:08:02 +00001804 }
1805 else {
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001806 // Update the set of live symbols.
Zhongxing Xu13d50172009-10-11 08:08:02 +00001807 for (SVal::symbol_iterator SI=V->symbol_begin(), SE=V->symbol_end();
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001808 SI!=SE;++SI)
1809 SymReaper.markLive(*SI);
1810
Zhongxing Xu13d50172009-10-11 08:08:02 +00001811 // If V is a region, then add it to the worklist.
1812 if (const MemRegion *RX = V->getAsRegion())
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001813 WorkList.push_back(std::make_pair(state_N, RX));
Ted Kremenek9e17cc62009-09-29 06:35:00 +00001814 }
1815 }
1816 }
1817
Ted Kremenek01756192009-10-29 05:14:17 +00001818 // See if any postponed SymbolicRegions are actually live now, after
1819 // having done a scan.
1820 for (llvm::SmallVectorImpl<RBDNode>::iterator I = Postponed.begin(),
1821 E = Postponed.end() ; I != E ; ++I) {
1822 if (const SymbolicRegion *SR = cast_or_null<SymbolicRegion>(I->second)) {
1823 if (SymReaper.isLive(SR->getSymbol())) {
1824 WorkList.push_back(*I);
1825 I->second = NULL;
1826 }
1827 }
1828 }
1829
1830 if (!WorkList.empty())
1831 goto tryAgain;
1832
Ted Kremenek9af46f52009-06-16 22:36:44 +00001833 // We have now scanned the store, marking reachable regions and symbols
1834 // as live. We now remove all the regions that are dead from the store
Mike Stump1eb44332009-09-09 15:08:12 +00001835 // as well as update DSymbols with the set symbols that are now dead.
Ted Kremenek451ac092009-08-06 04:50:20 +00001836 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenek9af46f52009-06-16 22:36:44 +00001837 const MemRegion* R = I.getKey();
Ted Kremenek9af46f52009-06-16 22:36:44 +00001838 // If this region live? Is so, none of its symbols are dead.
Zhongxing Xuf77869f2009-10-17 08:39:24 +00001839 if (Visited.count(std::make_pair(&state, R)))
Ted Kremenek9af46f52009-06-16 22:36:44 +00001840 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Ted Kremenek9af46f52009-06-16 22:36:44 +00001842 // Remove this dead region from the store.
Zhongxing Xud91ee272009-06-23 09:02:15 +00001843 store = Remove(store, ValMgr.makeLoc(R));
Mike Stump1eb44332009-09-09 15:08:12 +00001844
Ted Kremenek9af46f52009-06-16 22:36:44 +00001845 // Mark all non-live symbols that this region references as dead.
1846 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
1847 SymReaper.maybeDead(SymR->getSymbol());
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Zhongxing Xu13d50172009-10-11 08:08:02 +00001849 SVal X = *I.getData().getValue();
Ted Kremenek093569c2009-08-02 05:00:15 +00001850 SVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
1851 for (; SI != SE; ++SI)
1852 SymReaper.maybeDead(*SI);
1853 }
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Ted Kremenek2f26bc32009-08-02 04:45:08 +00001855 // Write the store back.
1856 state.setStore(store);
Ted Kremenek9af46f52009-06-16 22:36:44 +00001857}
1858
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001859GRState const *RegionStoreManager::EnterStackFrame(GRState const *state,
1860 StackFrameContext const *frame) {
1861 FunctionDecl const *FD = cast<FunctionDecl>(frame->getDecl());
1862 CallExpr const *CE = cast<CallExpr>(frame->getCallSite());
1863
1864 FunctionDecl::param_const_iterator PI = FD->param_begin();
1865
1866 CallExpr::const_arg_iterator AI = CE->arg_begin(), AE = CE->arg_end();
1867
1868 // Copy the arg expression value to the arg variables.
1869 for (; AI != AE; ++AI, ++PI) {
1870 SVal ArgVal = state->getSVal(*AI);
Ted Kremenekb48ad642009-12-04 00:26:31 +00001871 state = Bind(state, ValMgr.makeLoc(MRMgr.getVarRegion(*PI, frame)), ArgVal);
Zhongxing Xu4e3c1f72009-10-13 02:24:55 +00001872 }
1873
1874 return state;
1875}
1876
Ted Kremenek9af46f52009-06-16 22:36:44 +00001877//===----------------------------------------------------------------------===//
1878// Utility methods.
1879//===----------------------------------------------------------------------===//
1880
Ted Kremenek53ba0b62009-06-24 23:06:47 +00001881void RegionStoreManager::print(Store store, llvm::raw_ostream& OS,
Ted Kremenek9af46f52009-06-16 22:36:44 +00001882 const char* nl, const char *sep) {
Ted Kremenek451ac092009-08-06 04:50:20 +00001883 RegionBindings B = GetRegionBindings(store);
Ted Kremenekab22ee92009-10-20 01:20:57 +00001884 OS << "Store (direct and default bindings):" << nl;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Ted Kremenek451ac092009-08-06 04:50:20 +00001886 for (RegionBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001887 OS << ' ' << I.getKey() << " : " << I.getData() << nl;
Ted Kremenek9af46f52009-06-16 22:36:44 +00001888}