blob: e2dd176139d38644a249cdb034c4db1c54fd81e7 [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"
18#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21
22#include "llvm/ADT/ImmutableMap.h"
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000023#include "llvm/ADT/ImmutableList.h"
Zhongxing Xua071eb02008-10-24 06:01:33 +000024#include "llvm/Support/raw_ostream.h"
Zhongxing Xu17892752008-10-08 02:50:44 +000025#include "llvm/Support/Compiler.h"
26
27using namespace clang;
28
Zhongxing Xubaf03a72008-11-24 09:44:56 +000029// Actual Store type.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000030typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy;
Zhongxing Xubaf03a72008-11-24 09:44:56 +000031
32// RegionView GDM stuff.
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000033typedef llvm::ImmutableList<const MemRegion*> RegionViewTy;
34typedef llvm::ImmutableMap<const MemRegion*, RegionViewTy> RegionViewMapTy;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000035static int RegionViewMapTyIndex = 0;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000036namespace clang {
37template<> struct GRStateTrait<RegionViewMapTy>
38 : public GRStatePartialTrait<RegionViewMapTy> {
39 static void* GDMIndex() { return &RegionViewMapTyIndex; }
40};
41}
Zhongxing Xu17892752008-10-08 02:50:44 +000042
Zhongxing Xubaf03a72008-11-24 09:44:56 +000043// RegionExtents GDM stuff.
44// Currently RegionExtents are in bytes. We can change this representation when
45// there are real requirements.
46typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionExtentsTy;
47static int RegionExtentsTyIndex = 0;
48namespace clang {
49template<> struct GRStateTrait<RegionExtentsTy>
50 : public GRStatePartialTrait<RegionExtentsTy> {
51 static void* GDMIndex() { return &RegionExtentsTyIndex; }
52};
53}
54
Zhongxing Xu17892752008-10-08 02:50:44 +000055namespace {
56
57class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager {
58 RegionBindingsTy::Factory RBFactory;
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000059 RegionViewTy::Factory RVFactory;
60 RegionViewMapTy::Factory RVMFactory;
61
Zhongxing Xu17892752008-10-08 02:50:44 +000062 GRStateManager& StateMgr;
63 MemRegionManager MRMgr;
64
65public:
66 RegionStoreManager(GRStateManager& mgr)
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000067 : RBFactory(mgr.getAllocator()),
68 RVFactory(mgr.getAllocator()),
69 RVMFactory(mgr.getAllocator()),
70 StateMgr(mgr),
71 MRMgr(StateMgr.getAllocator()) {}
Zhongxing Xu17892752008-10-08 02:50:44 +000072
73 virtual ~RegionStoreManager() {}
74
Zhongxing Xu24194ef2008-10-24 01:38:55 +000075 MemRegionManager& getRegionManager() { return MRMgr; }
76
77 // FIXME: Is this function necessary?
78 SVal GetRegionSVal(Store St, const MemRegion* R) {
79 return Retrieve(St, loc::MemRegionVal(R));
80 }
Ted Kremenek4f090272008-10-27 21:54:31 +000081
Zhongxing Xuf22679e2008-11-07 10:38:33 +000082 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* CL, SVal V);
Zhongxing Xu24194ef2008-10-24 01:38:55 +000083
Zhongxing Xu143bf822008-10-25 14:18:57 +000084 SVal getLValueString(const GRState* St, const StringLiteral* S);
85
Zhongxing Xuf22679e2008-11-07 10:38:33 +000086 SVal getLValueCompoundLiteral(const GRState* St, const CompoundLiteralExpr*);
87
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +000088 SVal getLValueVar(const GRState* St, const VarDecl* VD);
89
90 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
91
92 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
93
Zhongxing Xub1d542a2008-10-24 01:09:32 +000094 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
95
Zhongxing Xue8a964b2008-11-22 13:21:46 +000096 SVal getSizeInElements(const GRState* St, const MemRegion* R);
97
Zhongxing Xub1d542a2008-10-24 01:09:32 +000098 SVal ArrayToPointer(SVal Array);
99
Zhongxing Xucb529b52008-11-16 07:06:26 +0000100 std::pair<const GRState*, SVal>
101 CastRegion(const GRState* St, SVal VoidPtr, QualType CastToTy, Stmt* CastE);
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000102
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000103 SVal Retrieve(Store S, Loc L, QualType T = QualType());
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000104
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000105 Store Bind(Store St, Loc LV, SVal V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000106
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000107 Store Remove(Store store, Loc LV) {
108 // FIXME: Implement.
109 return store;
110 }
111
Zhongxing Xu17892752008-10-08 02:50:44 +0000112 Store getInitialStore();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000113
114 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
115 /// 'this' object (C++). When used when analyzing a normal function this
116 /// method returns NULL.
117 const MemRegion* getSelfRegion(Store) {
118 assert (false && "Not implemented.");
119 return 0;
120 }
Zhongxing Xu17892752008-10-08 02:50:44 +0000121
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000122 Store RemoveDeadBindings(Store store, Stmt* Loc, const LiveVariables& Live,
123 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000124 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000125
Ted Kremenek42577d12008-11-12 19:18:35 +0000126 Store BindDecl(Store store, const VarDecl* VD, SVal* InitVal, unsigned Count);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000127
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000128 const GRState* setExtent(const GRState* St, const MemRegion* R, SVal Extent);
129
Zhongxing Xu17892752008-10-08 02:50:44 +0000130 static inline RegionBindingsTy GetRegionBindings(Store store) {
131 return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store));
132 }
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000133
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000134 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000135
136 void iterBindings(Store store, BindingsHandler& f) {
137 // FIXME: Implement.
138 }
Zhongxing Xua82512a2008-10-24 08:42:28 +0000139
140private:
141 Loc getVarLoc(const VarDecl* VD) {
142 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
143 }
144
Zhongxing Xud463d442008-11-02 12:13:30 +0000145 Store InitializeArray(Store store, const TypedRegion* R, SVal Init);
146 Store BindArrayToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000147 Store BindArrayToSymVal(Store store, const TypedRegion* BaseR);
148
Zhongxing Xud463d442008-11-02 12:13:30 +0000149 Store InitializeStruct(Store store, const TypedRegion* R, SVal Init);
150 Store BindStructToVal(Store store, const TypedRegion* BaseR, SVal V);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000151 Store BindStructToSymVal(Store store, const TypedRegion* BaseR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000152
153 SVal RetrieveStruct(Store store, const TypedRegion* R);
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000154 Store BindStruct(Store store, const TypedRegion* R, SVal V);
Zhongxing Xu63123d82008-11-23 04:30:35 +0000155
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000156 // Utility methods.
157 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
158 ASTContext& getContext() { return StateMgr.getContext(); }
Zhongxing Xu63123d82008-11-23 04:30:35 +0000159 SymbolManager& getSymbolManager() { return StateMgr.getSymbolManager(); }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000160
161 const GRState* AddRegionView(const GRState* St,
162 const MemRegion* View, const MemRegion* Base);
Zhongxing Xu17892752008-10-08 02:50:44 +0000163};
164
165} // end anonymous namespace
166
Ted Kremenek95c7b002008-10-24 01:04:59 +0000167StoreManager* clang::CreateRegionStoreManager(GRStateManager& StMgr) {
Zhongxing Xu24194ef2008-10-24 01:38:55 +0000168 return new RegionStoreManager(StMgr);
Ted Kremenek95c7b002008-10-24 01:04:59 +0000169}
170
Zhongxing Xu143bf822008-10-25 14:18:57 +0000171SVal RegionStoreManager::getLValueString(const GRState* St,
172 const StringLiteral* S) {
173 return loc::MemRegionVal(MRMgr.getStringRegion(S));
174}
175
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000176SVal RegionStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
177 return loc::MemRegionVal(MRMgr.getVarRegion(VD));
178}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000179
180SVal RegionStoreManager::getLValueCompoundLiteral(const GRState* St,
181 const CompoundLiteralExpr* CL) {
182 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL));
183}
184
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000185SVal RegionStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
186 SVal Base) {
187 return UnknownVal();
188}
189
190SVal RegionStoreManager::getLValueField(const GRState* St, SVal Base,
191 const FieldDecl* D) {
192 if (Base.isUnknownOrUndef())
193 return Base;
194
195 Loc BaseL = cast<Loc>(Base);
196 const MemRegion* BaseR = 0;
197
198 switch (BaseL.getSubKind()) {
199 case loc::MemRegionKind:
200 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
201 break;
202
203 case loc::SymbolValKind:
204 BaseR = MRMgr.getSymbolicRegion(cast<loc::SymbolVal>(&BaseL)->getSymbol());
205 break;
206
207 case loc::GotoLabelKind:
208 case loc::FuncValKind:
209 // These are anormal cases. Flag an undefined value.
210 return UndefinedVal();
211
212 case loc::ConcreteIntKind:
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000213 // While these seem funny, this can happen through casts.
214 // FIXME: What we should return is the field offset. For example,
215 // add the field offset to the integer value. That way funny things
216 // like this work properly: &(((struct foo *) 0xa)->f)
217 return Base;
218
219 default:
Zhongxing Xu13d1ee22008-11-07 08:57:30 +0000220 assert(0 && "Unhandled Base.");
Zhongxing Xuc4bf72c2008-10-22 13:44:38 +0000221 return Base;
222 }
223
224 return loc::MemRegionVal(MRMgr.getFieldRegion(D, BaseR));
225}
226
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000227SVal RegionStoreManager::getLValueElement(const GRState* St,
228 SVal Base, SVal Offset) {
229 if (Base.isUnknownOrUndef())
230 return Base;
231
Zhongxing Xu4a1513e2008-10-27 12:23:17 +0000232 if (isa<loc::SymbolVal>(Base))
233 return Base;
234
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000235 loc::MemRegionVal& BaseL = cast<loc::MemRegionVal>(Base);
236
Zhongxing Xue4d13932008-11-13 09:48:44 +0000237 // Pointer of any type can be cast and used as array base. We do not support
238 // that case yet.
239 if (!isa<ElementRegion>(BaseL.getRegion())) {
240 // Record what we have seen in real code.
241 assert(isa<FieldRegion>(BaseL.getRegion()));
242 return UnknownVal();
243 }
244
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000245 // We expect BaseR is an ElementRegion, not a base VarRegion.
246
247 const ElementRegion* ElemR = cast<ElementRegion>(BaseL.getRegion());
248
249 SVal Idx = ElemR->getIndex();
250
251 nonloc::ConcreteInt *CI1, *CI2;
252
253 // Only handle integer indices for now.
254 if ((CI1 = dyn_cast<nonloc::ConcreteInt>(&Idx)) &&
255 (CI2 = dyn_cast<nonloc::ConcreteInt>(&Offset))) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000256
Sebastian Redle95db4f2008-11-24 19:35:33 +0000257 // Temporary SVal to hold a potential signed and extended APSInt.
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000258 SVal SignedInt;
259
Sebastian Redle95db4f2008-11-24 19:35:33 +0000260 // Index might be unsigned. We have to convert it to signed. It might also
261 // be less wide than the size. We have to extend it.
262 if (CI2->getValue().isUnsigned() ||
263 CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth()) {
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000264 llvm::APSInt SI = CI2->getValue();
Sebastian Redlddee68b2008-11-24 19:39:40 +0000265 if (CI2->getValue().getBitWidth() < CI1->getValue().getBitWidth())
266 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000267 SI.setIsSigned(true);
268 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
269 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
270 }
271
Zhongxing Xu63123d82008-11-23 04:30:35 +0000272 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000273 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
274 ElemR->getSuperRegion()));
275 }
276
277 return UnknownVal();
278}
279
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000280SVal RegionStoreManager::getSizeInElements(const GRState* St,
281 const MemRegion* R) {
282 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
283 // Get the type of the variable.
284 QualType T = VR->getType(getContext());
285
286 // It must be of array type.
287 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
288
289 // return the size as signed integer.
290 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
291 }
292
293 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000294 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000295 // We intentionally made the size value signed because it participates in
296 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000297 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000298 }
299
300 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000301 GRStateRef state(St, StateMgr);
302
303 // Get the size of the super region in bytes.
304 RegionExtentsTy::data_type* T
305 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
306
307 assert(T && "region extent not exist");
308
309 // Assume it's ConcreteInt for now.
310 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
311
312 // Get the size of the element in bits.
313 QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr())
314 ->getPointeeType();
315
316 uint64_t X = getContext().getTypeSize(ElemTy);
317
318 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
319 false);
320
321 // Calculate the number of elements.
322
323 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
324 // signed?
325 if (SSize.isUnsigned())
326 SSize.setIsSigned(true);
327
328 // FIXME: move this operation into BasicVals.
329 const llvm::APSInt S =
330 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
331
332 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000333 }
334
335 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
336 // FIXME: Unsupported yet.
337 FR = 0;
338 return UnknownVal();
339 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000340
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000341 assert(0 && "Other regions are not supported yet.");
342}
343
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000344// Cast 'pointer to array' to 'pointer to the first element of array'.
345
346SVal RegionStoreManager::ArrayToPointer(SVal Array) {
347 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000348
Zhongxing Xu63123d82008-11-23 04:30:35 +0000349 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000350 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
351
352 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000353}
354
Zhongxing Xucb529b52008-11-16 07:06:26 +0000355std::pair<const GRState*, SVal>
356RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
357 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000358 if (const AllocaRegion* AR =
359 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
360
361 // Create a new region to attach type information to it.
362 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
363
364 // Get the pointer to the first element.
365 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
366 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
367
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000368 // Add a RegionView to base region.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000369 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
370 loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000371 }
372
373 // Default case.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000374 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000375}
376
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000377SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000378 assert(!isa<UnknownVal>(L) && "location unknown");
379 assert(!isa<UndefinedVal>(L) && "location undefined");
380
381 switch (L.getSubKind()) {
382 case loc::MemRegionKind: {
383 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
384 assert(R && "bad region");
385
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000386 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
387 if (TR->getType(getContext())->isStructureType())
388 return RetrieveStruct(S, TR);
389
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000390 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
391 RegionBindingsTy::data_type* V = B.lookup(R);
392 return V ? *V : UnknownVal();
393 }
394
395 case loc::SymbolValKind:
396 return UnknownVal();
397
398 case loc::ConcreteIntKind:
399 return UndefinedVal(); // As in BasicStoreManager.
400
401 case loc::FuncValKind:
402 return L;
403
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000404 default:
405 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000406 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000407 }
408}
409
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000410SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
411 QualType T = R->getType(getContext());
412 assert(T->isStructureType());
413
414 const RecordType* RT = cast<RecordType>(T.getTypePtr());
415 RecordDecl* RD = RT->getDecl();
416 assert(RD->isDefinition());
417
418 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
419
420 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
421 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
422 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000423 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000424
425 SVal FieldValue = data ? *data : UnknownVal();
426
427 StructVal = getBasicVals().consVals(FieldValue, StructVal);
428 }
429
430 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
431}
432
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000433Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000434 if (LV.getSubKind() == loc::SymbolValKind)
435 return store;
436
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000437 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000438
Ted Kremenek993f1c72008-10-17 20:28:54 +0000439 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000440
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000441 assert(R);
442
443 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
444 if (TR->getType(getContext())->isStructureType())
445 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000446
447 RegionBindingsTy B = GetRegionBindings(store);
448 return V.isUnknown()
449 ? RBFactory.Remove(B, R).getRoot()
450 : RBFactory.Add(B, R, V).getRoot();
451}
452
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000453Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
454 QualType T = R->getType(getContext());
455 assert(T->isStructureType());
456
457 const RecordType* RT = cast<RecordType>(T.getTypePtr());
458 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000459
460 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000461 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000462 // function argument.
463 assert(V.isUnknown());
464 return store;
465 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000466
467 RegionBindingsTy B = GetRegionBindings(store);
468
Zhongxing Xud463d442008-11-02 12:13:30 +0000469 if (isa<UnknownVal>(V))
470 return BindStructToVal(store, R, UnknownVal());
471
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000472 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
473
474 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
475 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
476
477 for (; FI != FE; ++FI, ++VI) {
478 assert(VI != VE);
479
480 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
481
482 B = RBFactory.Add(B, FR, *VI);
483 }
484
485 return B.getRoot();
486}
487
Zhongxing Xu17892752008-10-08 02:50:44 +0000488Store RegionStoreManager::getInitialStore() {
489 typedef LiveVariables::AnalysisDataTy LVDataTy;
490 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
491
492 Store St = RBFactory.GetEmptyMap().getRoot();
493
494 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000495 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000496
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000497 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000498 // Punt on static variables for now.
499 if (VD->getStorageClass() == VarDecl::Static)
500 continue;
501
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000502 VarRegion* VR = MRMgr.getVarRegion(VD);
503
Zhongxing Xu17892752008-10-08 02:50:44 +0000504 QualType T = VD->getType();
505 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000506 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000507 // Initialize globals and parameters to symbolic values.
508 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000509 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000510 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000511 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000512 : UndefinedVal();
513
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000514 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000515 }
516 else if (T->isArrayType()) {
517 if (VD->hasGlobalStorage()) // Params cannot have array type.
518 St = BindArrayToSymVal(St, VR);
519 else
520 St = BindArrayToVal(St, VR, UndefinedVal());
521 }
522 else if (T->isStructureType()) {
523 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
524 isa<ImplicitParamDecl>(VD))
525 St = BindStructToSymVal(St, VR);
526 else
527 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000528 }
529 }
530 }
531 return St;
532}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000533
Ted Kremenek42577d12008-11-12 19:18:35 +0000534Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
535 SVal* InitVal, unsigned Count) {
536
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000537 if (VD->hasGlobalStorage()) {
538 // Static global variables should not be visited here.
539 assert(!(VD->getStorageClass() == VarDecl::Static &&
540 VD->isFileVarDecl()));
541 // Process static variables.
542 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000543 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000544 // Only handle pointer and integer static variables.
545
546 QualType T = VD->getType();
547
548 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000549 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000550 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000551
552 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000553 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000554 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000555
556 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000557 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000558 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000559 }
560 }
561 } else {
562 // Process local variables.
563
564 QualType T = VD->getType();
565
Zhongxing Xua82512a2008-10-24 08:42:28 +0000566 VarRegion* VR = MRMgr.getVarRegion(VD);
567
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000568 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000569 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000570 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000571 }
572 else if (T->isArrayType()) {
573 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000574 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000575 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000576 store = InitializeArray(store, VR, *InitVal);
577 }
578 else if (T->isStructureType()) {
579 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000580 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000581 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000582 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000583 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000584
585 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000586 }
587 return store;
588}
589
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000590Store RegionStoreManager::BindCompoundLiteral(Store store,
591 const CompoundLiteralExpr* CL,
592 SVal V) {
593 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
594 store = Bind(store, loc::MemRegionVal(R), V);
595 return store;
596}
597
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000598const GRState* RegionStoreManager::setExtent(const GRState* St,
599 const MemRegion* R, SVal Extent) {
600 GRStateRef state(St, StateMgr);
601 return state.set<RegionExtentsTy>(R, Extent);
602}
603
604
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000605Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
606 const LiveVariables& Live,
607 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
608 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
609
610 RegionBindingsTy B = GetRegionBindings(store);
611 typedef SVal::symbol_iterator symbol_iterator;
612
613 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
614 // in SymbolicRegions.
615 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
616 SVal X = I.getData();
617 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
618 LSymbols.insert(*SI);
619 }
620
621 return store;
622}
623
Zhongxing Xua071eb02008-10-24 06:01:33 +0000624void RegionStoreManager::print(Store store, std::ostream& Out,
625 const char* nl, const char *sep) {
626 llvm::raw_os_ostream OS(Out);
627 RegionBindingsTy B = GetRegionBindings(store);
628 OS << "Store:" << nl;
629
630 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
631 OS << ' '; I.getKey()->print(OS); OS << " : ";
632 I.getData().print(OS); OS << nl;
633 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000634}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000635
Zhongxing Xud463d442008-11-02 12:13:30 +0000636Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000637 SVal Init) {
638 QualType T = R->getType(getContext());
639 assert(T->isArrayType());
640
641 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
642
643 llvm::APInt Size = CAT->getSize();
644
645 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
646
647 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
648
649 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
650
651 for (; i != Size; ++i) {
652 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
653
654 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
655
656 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
657 // The init list might be shorter than the array decl.
658 if (VI != VE) ++VI;
659 }
660
661 return store;
662}
663
Zhongxing Xud463d442008-11-02 12:13:30 +0000664// Bind all elements of the array to some value.
665Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
666 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000667 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000668 assert(T->isArrayType());
669
Zhongxing Xua82512a2008-10-24 08:42:28 +0000670 // Only handle constant size array for now.
671 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
672
673 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000674 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
675 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000676 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000677
678 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
679
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000680 if (CAT->getElementType()->isStructureType())
681 store = BindStructToVal(store, ER, V);
682 else
683 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000684 }
685 }
686
687 return store;
688}
689
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000690Store RegionStoreManager::BindArrayToSymVal(Store store,
691 const TypedRegion* BaseR) {
692 QualType T = BaseR->getType(getContext());
693 assert(T->isArrayType());
694
695 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
696 llvm::APInt Size = CAT->getSize();
697 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
698 for (; i != Size; ++i) {
699 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
700
701 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
702
703 if (CAT->getElementType()->isStructureType()) {
704 store = BindStructToSymVal(store, ER);
705 }
706 else {
707 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
708 &Idx.getValue(), CAT->getElementType());
709 store = Bind(store, loc::MemRegionVal(ER), V);
710 }
711 }
712 }
713
714 return store;
715}
716
Zhongxing Xud463d442008-11-02 12:13:30 +0000717Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000718 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000719 QualType T = R->getType(getContext());
720 assert(T->isStructureType());
721
722 RecordType* RT = cast<RecordType>(T.getTypePtr());
723 RecordDecl* RD = RT->getDecl();
724 assert(RD->isDefinition());
725
726 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
727 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
728 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
729
730 for (; FI != FE; ++FI) {
731 QualType FTy = (*FI)->getType();
732 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
733
734 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
735 if (VI != VE) {
736 store = Bind(store, loc::MemRegionVal(FR), *VI);
737 ++VI;
738 } else
739 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
740 }
741 else if (FTy->isArrayType()) {
742 if (VI != VE) {
743 store = InitializeArray(store, FR, *VI);
744 ++VI;
745 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000746 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000747 }
748 else if (FTy->isStructureType()) {
749 if (VI != VE) {
750 store = InitializeStruct(store, FR, *VI);
751 ++VI;
752 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000753 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000754 }
755 }
756 return store;
757}
758
Zhongxing Xud463d442008-11-02 12:13:30 +0000759// Bind all fields of the struct to some value.
760Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
761 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000762 QualType T = BaseR->getType(getContext());
763 assert(T->isStructureType());
764
765 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000766 RecordDecl* RD = RT->getDecl();
767 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000768
769 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
770
771 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000772
773 QualType FTy = (*I)->getType();
774 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
775
776 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000777 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000778
779 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000780 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000781
782 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000783 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000784 }
785 }
786
787 return store;
788}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000789
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000790Store RegionStoreManager::BindStructToSymVal(Store store,
791 const TypedRegion* BaseR) {
792 QualType T = BaseR->getType(getContext());
793 assert(T->isStructureType());
794
795 const RecordType* RT = cast<RecordType>(T.getTypePtr());
796 RecordDecl* RD = RT->getDecl();
797 assert(RD->isDefinition());
798
799 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
800
801 for (; I != E; ++I) {
802 QualType FTy = (*I)->getType();
803 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
804
805 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
806 store = Bind(store, loc::MemRegionVal(FR),
807 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
808 }
809 else if (FTy->isArrayType()) {
810 store = BindArrayToSymVal(store, FR);
811 }
812 else if (FTy->isStructureType()) {
813 store = BindStructToSymVal(store, FR);
814 }
815 }
816
817 return store;
818}
819
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000820const GRState* RegionStoreManager::AddRegionView(const GRState* St,
821 const MemRegion* View,
822 const MemRegion* Base) {
823 GRStateRef state(St, StateMgr);
824
825 // First, retrieve the region view of the base region.
826 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
827 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
828
829 // Now add View to the region view.
830 L = RVFactory.Add(View, L);
831
832 // Create a new state with the new region view.
833 return state.set<RegionViewMapTy>(Base, L);
834}