blob: 747b16d7b6d9ed23676506a99929b8a04f36128b [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 Redle95db4f2008-11-24 19:35:33 +0000265 SI.extend(CI1->getValue().getBitWidth());
Zhongxing Xucc0d0ec2008-11-13 09:15:14 +0000266 SI.setIsSigned(true);
267 SignedInt = nonloc::ConcreteInt(getBasicVals().getValue(SI));
268 CI2 = cast<nonloc::ConcreteInt>(&SignedInt);
269 }
270
Zhongxing Xu63123d82008-11-23 04:30:35 +0000271 SVal NewIdx = CI1->EvalBinOp(getBasicVals(), BinaryOperator::Add, *CI2);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000272 return loc::MemRegionVal(MRMgr.getElementRegion(NewIdx,
273 ElemR->getSuperRegion()));
274 }
275
276 return UnknownVal();
277}
278
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000279SVal RegionStoreManager::getSizeInElements(const GRState* St,
280 const MemRegion* R) {
281 if (const VarRegion* VR = dyn_cast<VarRegion>(R)) {
282 // Get the type of the variable.
283 QualType T = VR->getType(getContext());
284
285 // It must be of array type.
286 const ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
287
288 // return the size as signed integer.
289 return NonLoc::MakeVal(getBasicVals(), CAT->getSize(), false);
290 }
291
292 if (const StringRegion* SR = dyn_cast<StringRegion>(R)) {
Zhongxing Xu6613d082008-11-24 02:18:56 +0000293 const StringLiteral* Str = SR->getStringLiteral();
Zhongxing Xud0fd3b72008-11-24 02:30:48 +0000294 // We intentionally made the size value signed because it participates in
295 // operations with signed indices.
Zhongxing Xu4b89e032008-11-24 05:16:01 +0000296 return NonLoc::MakeVal(getBasicVals(), Str->getByteLength() + 1, false);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000297 }
298
299 if (const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R)) {
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000300 GRStateRef state(St, StateMgr);
301
302 // Get the size of the super region in bytes.
303 RegionExtentsTy::data_type* T
304 = state.get<RegionExtentsTy>(ATR->getSuperRegion());
305
306 assert(T && "region extent not exist");
307
308 // Assume it's ConcreteInt for now.
309 llvm::APSInt SSize = cast<nonloc::ConcreteInt>(*T).getValue();
310
311 // Get the size of the element in bits.
312 QualType ElemTy = cast<PointerType>(ATR->getType(getContext()).getTypePtr())
313 ->getPointeeType();
314
315 uint64_t X = getContext().getTypeSize(ElemTy);
316
317 const llvm::APSInt& ESize = getBasicVals().getValue(X, SSize.getBitWidth(),
318 false);
319
320 // Calculate the number of elements.
321
322 // FIXME: What do we do with signed-ness problem? Shall we make all APSInts
323 // signed?
324 if (SSize.isUnsigned())
325 SSize.setIsSigned(true);
326
327 // FIXME: move this operation into BasicVals.
328 const llvm::APSInt S =
329 (SSize * getBasicVals().getValue(8, SSize.getBitWidth(), false)) / ESize;
330
331 return NonLoc::MakeVal(getBasicVals(), S);
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000332 }
333
334 if (const FieldRegion* FR = dyn_cast<FieldRegion>(R)) {
335 // FIXME: Unsupported yet.
336 FR = 0;
337 return UnknownVal();
338 }
Zhongxing Xu369f4292008-11-22 13:23:00 +0000339
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000340 assert(0 && "Other regions are not supported yet.");
341}
342
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000343// Cast 'pointer to array' to 'pointer to the first element of array'.
344
345SVal RegionStoreManager::ArrayToPointer(SVal Array) {
346 const MemRegion* ArrayR = cast<loc::MemRegionVal>(&Array)->getRegion();
Zhongxing Xu143bf822008-10-25 14:18:57 +0000347
Zhongxing Xu63123d82008-11-23 04:30:35 +0000348 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
Zhongxing Xu0b7e6422008-10-26 02:23:57 +0000349 ElementRegion* ER = MRMgr.getElementRegion(Idx, ArrayR);
350
351 return loc::MemRegionVal(ER);
Zhongxing Xub1d542a2008-10-24 01:09:32 +0000352}
353
Zhongxing Xucb529b52008-11-16 07:06:26 +0000354std::pair<const GRState*, SVal>
355RegionStoreManager::CastRegion(const GRState* St, SVal VoidPtr,
356 QualType CastToTy, Stmt* CastE) {
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000357 if (const AllocaRegion* AR =
358 dyn_cast<AllocaRegion>(cast<loc::MemRegionVal>(VoidPtr).getRegion())) {
359
360 // Create a new region to attach type information to it.
361 const AnonTypedRegion* TR = MRMgr.getAnonTypedRegion(CastToTy, AR);
362
363 // Get the pointer to the first element.
364 nonloc::ConcreteInt Idx(getBasicVals().getZeroWithPtrWidth(false));
365 const ElementRegion* ER = MRMgr.getElementRegion(Idx, TR);
366
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000367 // Add a RegionView to base region.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000368 return std::pair<const GRState*, SVal>(AddRegionView(St, TR, AR),
369 loc::MemRegionVal(ER));
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000370 }
371
372 // Default case.
Zhongxing Xucb529b52008-11-16 07:06:26 +0000373 return std::pair<const GRState*, SVal>(St, UnknownVal());
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000374}
375
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000376SVal RegionStoreManager::Retrieve(Store S, Loc L, QualType T) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000377 assert(!isa<UnknownVal>(L) && "location unknown");
378 assert(!isa<UndefinedVal>(L) && "location undefined");
379
380 switch (L.getSubKind()) {
381 case loc::MemRegionKind: {
382 const MemRegion* R = cast<loc::MemRegionVal>(L).getRegion();
383 assert(R && "bad region");
384
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000385 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
386 if (TR->getType(getContext())->isStructureType())
387 return RetrieveStruct(S, TR);
388
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000389 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(S));
390 RegionBindingsTy::data_type* V = B.lookup(R);
391 return V ? *V : UnknownVal();
392 }
393
394 case loc::SymbolValKind:
395 return UnknownVal();
396
397 case loc::ConcreteIntKind:
398 return UndefinedVal(); // As in BasicStoreManager.
399
400 case loc::FuncValKind:
401 return L;
402
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000403 default:
404 assert(false && "Invalid Location");
Ted Kremenekab7b32b2008-11-19 00:27:37 +0000405 return L;
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000406 }
407}
408
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000409SVal RegionStoreManager::RetrieveStruct(Store store, const TypedRegion* R) {
410 QualType T = R->getType(getContext());
411 assert(T->isStructureType());
412
413 const RecordType* RT = cast<RecordType>(T.getTypePtr());
414 RecordDecl* RD = RT->getDecl();
415 assert(RD->isDefinition());
416
417 llvm::ImmutableList<SVal> StructVal = getBasicVals().getEmptySValList();
418
419 for (int i = RD->getNumMembers() - 1; i >= 0; --i) {
420 FieldRegion* FR = MRMgr.getFieldRegion(RD->getMember(i), R);
421 RegionBindingsTy B(static_cast<const RegionBindingsTy::TreeTy*>(store));
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000422 RegionBindingsTy::data_type* data = B.lookup(FR);
Zhongxing Xu6e3f01c2008-10-31 07:16:08 +0000423
424 SVal FieldValue = data ? *data : UnknownVal();
425
426 StructVal = getBasicVals().consVals(FieldValue, StructVal);
427 }
428
429 return NonLoc::MakeCompoundVal(T, StructVal, getBasicVals());
430}
431
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000432Store RegionStoreManager::Bind(Store store, Loc LV, SVal V) {
Zhongxing Xu8fe63af2008-10-27 09:24:07 +0000433 if (LV.getSubKind() == loc::SymbolValKind)
434 return store;
435
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000436 assert(LV.getSubKind() == loc::MemRegionKind);
Zhongxing Xu17892752008-10-08 02:50:44 +0000437
Ted Kremenek993f1c72008-10-17 20:28:54 +0000438 const MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion();
Zhongxing Xu17892752008-10-08 02:50:44 +0000439
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000440 assert(R);
441
442 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R))
443 if (TR->getType(getContext())->isStructureType())
444 return BindStruct(store, TR, V);
Zhongxing Xu17892752008-10-08 02:50:44 +0000445
446 RegionBindingsTy B = GetRegionBindings(store);
447 return V.isUnknown()
448 ? RBFactory.Remove(B, R).getRoot()
449 : RBFactory.Add(B, R, V).getRoot();
450}
451
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000452Store RegionStoreManager::BindStruct(Store store, const TypedRegion* R, SVal V){
453 QualType T = R->getType(getContext());
454 assert(T->isStructureType());
455
456 const RecordType* RT = cast<RecordType>(T.getTypePtr());
457 RecordDecl* RD = RT->getDecl();
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000458
459 if (!RD->isDefinition()) {
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000460 // This can only occur when a pointer of incomplete struct type is used as a
Zhongxing Xua4f28ff2008-11-13 08:41:36 +0000461 // function argument.
462 assert(V.isUnknown());
463 return store;
464 }
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000465
466 RegionBindingsTy B = GetRegionBindings(store);
467
Zhongxing Xud463d442008-11-02 12:13:30 +0000468 if (isa<UnknownVal>(V))
469 return BindStructToVal(store, R, UnknownVal());
470
Zhongxing Xuf0dfa8d2008-10-31 08:10:01 +0000471 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(V);
472
473 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
474 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
475
476 for (; FI != FE; ++FI, ++VI) {
477 assert(VI != VE);
478
479 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
480
481 B = RBFactory.Add(B, FR, *VI);
482 }
483
484 return B.getRoot();
485}
486
Zhongxing Xu17892752008-10-08 02:50:44 +0000487Store RegionStoreManager::getInitialStore() {
488 typedef LiveVariables::AnalysisDataTy LVDataTy;
489 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
490
491 Store St = RBFactory.GetEmptyMap().getRoot();
492
493 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000494 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Zhongxing Xu17892752008-10-08 02:50:44 +0000495
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000496 if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000497 // Punt on static variables for now.
498 if (VD->getStorageClass() == VarDecl::Static)
499 continue;
500
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000501 VarRegion* VR = MRMgr.getVarRegion(VD);
502
Zhongxing Xu17892752008-10-08 02:50:44 +0000503 QualType T = VD->getType();
504 // Only handle pointers and integers for now.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000505 if (Loc::IsLocType(T) || T->isIntegerType()) {
Zhongxing Xu17892752008-10-08 02:50:44 +0000506 // Initialize globals and parameters to symbolic values.
507 // Initialize local variables to undefined.
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000508 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
Zhongxing Xu17892752008-10-08 02:50:44 +0000509 isa<ImplicitParamDecl>(VD))
Zhongxing Xu63123d82008-11-23 04:30:35 +0000510 ? SVal::GetSymbolValue(getSymbolManager(), VD)
Zhongxing Xu17892752008-10-08 02:50:44 +0000511 : UndefinedVal();
512
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000513 St = Bind(St, getVarLoc(VD), X);
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000514 }
515 else if (T->isArrayType()) {
516 if (VD->hasGlobalStorage()) // Params cannot have array type.
517 St = BindArrayToSymVal(St, VR);
518 else
519 St = BindArrayToVal(St, VR, UndefinedVal());
520 }
521 else if (T->isStructureType()) {
522 if (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
523 isa<ImplicitParamDecl>(VD))
524 St = BindStructToSymVal(St, VR);
525 else
526 St = BindStructToVal(St, VR, UndefinedVal());
Zhongxing Xu17892752008-10-08 02:50:44 +0000527 }
528 }
529 }
530 return St;
531}
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000532
Ted Kremenek42577d12008-11-12 19:18:35 +0000533Store RegionStoreManager::BindDecl(Store store, const VarDecl* VD,
534 SVal* InitVal, unsigned Count) {
535
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000536 if (VD->hasGlobalStorage()) {
537 // Static global variables should not be visited here.
538 assert(!(VD->getStorageClass() == VarDecl::Static &&
539 VD->isFileVarDecl()));
540 // Process static variables.
541 if (VD->getStorageClass() == VarDecl::Static) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000542 if (!InitVal) {
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000543 // Only handle pointer and integer static variables.
544
545 QualType T = VD->getType();
546
547 if (Loc::IsLocType(T))
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000548 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000549 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000550
551 else if (T->isIntegerType())
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000552 store = Bind(store, getVarLoc(VD),
Zhongxing Xu63123d82008-11-23 04:30:35 +0000553 loc::ConcreteInt(getBasicVals().getValue(0, T)));
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000554
555 // Other types of static local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000556 } else {
Ted Kremenek42577d12008-11-12 19:18:35 +0000557 store = Bind(store, getVarLoc(VD), *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000558 }
559 }
560 } else {
561 // Process local variables.
562
563 QualType T = VD->getType();
564
Zhongxing Xua82512a2008-10-24 08:42:28 +0000565 VarRegion* VR = MRMgr.getVarRegion(VD);
566
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000567 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000568 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xua82512a2008-10-24 08:42:28 +0000569 store = Bind(store, loc::MemRegionVal(VR), V);
Ted Kremenek42577d12008-11-12 19:18:35 +0000570 }
571 else if (T->isArrayType()) {
572 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000573 store = BindArrayToVal(store, VR, UndefinedVal());
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000574 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000575 store = InitializeArray(store, VR, *InitVal);
576 }
577 else if (T->isStructureType()) {
578 if (!InitVal)
Zhongxing Xud463d442008-11-02 12:13:30 +0000579 store = BindStructToVal(store, VR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000580 else
Ted Kremenek42577d12008-11-12 19:18:35 +0000581 store = InitializeStruct(store, VR, *InitVal);
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000582 }
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000583
584 // Other types of local variables are not handled yet.
Zhongxing Xu53bcdd42008-10-21 05:29:26 +0000585 }
586 return store;
587}
588
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000589Store RegionStoreManager::BindCompoundLiteral(Store store,
590 const CompoundLiteralExpr* CL,
591 SVal V) {
592 CompoundLiteralRegion* R = MRMgr.getCompoundLiteralRegion(CL);
593 store = Bind(store, loc::MemRegionVal(R), V);
594 return store;
595}
596
Zhongxing Xubaf03a72008-11-24 09:44:56 +0000597const GRState* RegionStoreManager::setExtent(const GRState* St,
598 const MemRegion* R, SVal Extent) {
599 GRStateRef state(St, StateMgr);
600 return state.set<RegionExtentsTy>(R, Extent);
601}
602
603
Zhongxing Xu8916d5b2008-11-10 09:39:04 +0000604Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
605 const LiveVariables& Live,
606 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots,
607 LiveSymbolsTy& LSymbols, DeadSymbolsTy& DSymbols) {
608
609 RegionBindingsTy B = GetRegionBindings(store);
610 typedef SVal::symbol_iterator symbol_iterator;
611
612 // FIXME: Mark all region binding value's symbol as live. We also omit symbols
613 // in SymbolicRegions.
614 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
615 SVal X = I.getData();
616 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
617 LSymbols.insert(*SI);
618 }
619
620 return store;
621}
622
Zhongxing Xua071eb02008-10-24 06:01:33 +0000623void RegionStoreManager::print(Store store, std::ostream& Out,
624 const char* nl, const char *sep) {
625 llvm::raw_os_ostream OS(Out);
626 RegionBindingsTy B = GetRegionBindings(store);
627 OS << "Store:" << nl;
628
629 for (RegionBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
630 OS << ' '; I.getKey()->print(OS); OS << " : ";
631 I.getData().print(OS); OS << nl;
632 }
Zhongxing Xu5b8b6f22008-10-24 04:33:15 +0000633}
Zhongxing Xua82512a2008-10-24 08:42:28 +0000634
Zhongxing Xud463d442008-11-02 12:13:30 +0000635Store RegionStoreManager::InitializeArray(Store store, const TypedRegion* R,
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000636 SVal Init) {
637 QualType T = R->getType(getContext());
638 assert(T->isArrayType());
639
640 ConstantArrayType* CAT = cast<ConstantArrayType>(T.getTypePtr());
641
642 llvm::APInt Size = CAT->getSize();
643
644 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
645
646 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
647
648 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
649
650 for (; i != Size; ++i) {
651 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
652
653 ElementRegion* ER = MRMgr.getElementRegion(Idx, R);
654
655 store = Bind(store, loc::MemRegionVal(ER), (VI!=VE) ? *VI : UndefinedVal());
656 // The init list might be shorter than the array decl.
657 if (VI != VE) ++VI;
658 }
659
660 return store;
661}
662
Zhongxing Xud463d442008-11-02 12:13:30 +0000663// Bind all elements of the array to some value.
664Store RegionStoreManager::BindArrayToVal(Store store, const TypedRegion* BaseR,
665 SVal V){
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000666 QualType T = BaseR->getType(getContext());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000667 assert(T->isArrayType());
668
Zhongxing Xua82512a2008-10-24 08:42:28 +0000669 // Only handle constant size array for now.
670 if (ConstantArrayType* CAT=dyn_cast<ConstantArrayType>(T.getTypePtr())) {
671
672 llvm::APInt Size = CAT->getSize();
Zhongxing Xu1a12a0e2008-10-31 10:24:47 +0000673 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
674 for (; i != Size; ++i) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000675 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
Zhongxing Xua82512a2008-10-24 08:42:28 +0000676
677 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
678
Zhongxing Xu9b6ceb12008-11-18 13:11:04 +0000679 if (CAT->getElementType()->isStructureType())
680 store = BindStructToVal(store, ER, V);
681 else
682 store = Bind(store, loc::MemRegionVal(ER), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000683 }
684 }
685
686 return store;
687}
688
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000689Store RegionStoreManager::BindArrayToSymVal(Store store,
690 const TypedRegion* BaseR) {
691 QualType T = BaseR->getType(getContext());
692 assert(T->isArrayType());
693
694 if (ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(T.getTypePtr())) {
695 llvm::APInt Size = CAT->getSize();
696 llvm::APInt i = llvm::APInt::getNullValue(Size.getBitWidth());
697 for (; i != Size; ++i) {
698 nonloc::ConcreteInt Idx(getBasicVals().getValue(llvm::APSInt(i)));
699
700 ElementRegion* ER = MRMgr.getElementRegion(Idx, BaseR);
701
702 if (CAT->getElementType()->isStructureType()) {
703 store = BindStructToSymVal(store, ER);
704 }
705 else {
706 SVal V = SVal::getSymbolValue(getSymbolManager(), BaseR,
707 &Idx.getValue(), CAT->getElementType());
708 store = Bind(store, loc::MemRegionVal(ER), V);
709 }
710 }
711 }
712
713 return store;
714}
715
Zhongxing Xud463d442008-11-02 12:13:30 +0000716Store RegionStoreManager::InitializeStruct(Store store, const TypedRegion* R,
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000717 SVal Init) {
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000718 QualType T = R->getType(getContext());
719 assert(T->isStructureType());
720
721 RecordType* RT = cast<RecordType>(T.getTypePtr());
722 RecordDecl* RD = RT->getDecl();
723 assert(RD->isDefinition());
724
725 nonloc::CompoundVal& CV = cast<nonloc::CompoundVal>(Init);
726 nonloc::CompoundVal::iterator VI = CV.begin(), VE = CV.end();
727 RecordDecl::field_iterator FI = RD->field_begin(), FE = RD->field_end();
728
729 for (; FI != FE; ++FI) {
730 QualType FTy = (*FI)->getType();
731 FieldRegion* FR = MRMgr.getFieldRegion(*FI, R);
732
733 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
734 if (VI != VE) {
735 store = Bind(store, loc::MemRegionVal(FR), *VI);
736 ++VI;
737 } else
738 store = Bind(store, loc::MemRegionVal(FR), UndefinedVal());
739 }
740 else if (FTy->isArrayType()) {
741 if (VI != VE) {
742 store = InitializeArray(store, FR, *VI);
743 ++VI;
744 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000745 store = BindArrayToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000746 }
747 else if (FTy->isStructureType()) {
748 if (VI != VE) {
749 store = InitializeStruct(store, FR, *VI);
750 ++VI;
751 } else
Zhongxing Xud463d442008-11-02 12:13:30 +0000752 store = BindStructToVal(store, FR, UndefinedVal());
Zhongxing Xuaf0a8442008-10-31 10:53:01 +0000753 }
754 }
755 return store;
756}
757
Zhongxing Xud463d442008-11-02 12:13:30 +0000758// Bind all fields of the struct to some value.
759Store RegionStoreManager::BindStructToVal(Store store, const TypedRegion* BaseR,
760 SVal V) {
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000761 QualType T = BaseR->getType(getContext());
762 assert(T->isStructureType());
763
764 const RecordType* RT = cast<RecordType>(T.getTypePtr());
Zhongxing Xua82512a2008-10-24 08:42:28 +0000765 RecordDecl* RD = RT->getDecl();
766 assert(RD->isDefinition());
Zhongxing Xuea8a1852008-10-31 11:02:48 +0000767
768 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
769
770 for (; I != E; ++I) {
Zhongxing Xua82512a2008-10-24 08:42:28 +0000771
772 QualType FTy = (*I)->getType();
773 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
774
775 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000776 store = Bind(store, loc::MemRegionVal(FR), V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000777
778 } else if (FTy->isArrayType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000779 store = BindArrayToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000780
781 } else if (FTy->isStructureType()) {
Zhongxing Xud463d442008-11-02 12:13:30 +0000782 store = BindStructToVal(store, FR, V);
Zhongxing Xua82512a2008-10-24 08:42:28 +0000783 }
784 }
785
786 return store;
787}
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000788
Zhongxing Xuc3a05992008-11-19 11:06:24 +0000789Store RegionStoreManager::BindStructToSymVal(Store store,
790 const TypedRegion* BaseR) {
791 QualType T = BaseR->getType(getContext());
792 assert(T->isStructureType());
793
794 const RecordType* RT = cast<RecordType>(T.getTypePtr());
795 RecordDecl* RD = RT->getDecl();
796 assert(RD->isDefinition());
797
798 RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
799
800 for (; I != E; ++I) {
801 QualType FTy = (*I)->getType();
802 FieldRegion* FR = MRMgr.getFieldRegion(*I, BaseR);
803
804 if (Loc::IsLocType(FTy) || FTy->isIntegerType()) {
805 store = Bind(store, loc::MemRegionVal(FR),
806 SVal::getSymbolValue(getSymbolManager(), BaseR, *I, FTy));
807 }
808 else if (FTy->isArrayType()) {
809 store = BindArrayToSymVal(store, FR);
810 }
811 else if (FTy->isStructureType()) {
812 store = BindStructToSymVal(store, FR);
813 }
814 }
815
816 return store;
817}
818
Zhongxing Xudc0a25d2008-11-16 04:07:26 +0000819const GRState* RegionStoreManager::AddRegionView(const GRState* St,
820 const MemRegion* View,
821 const MemRegion* Base) {
822 GRStateRef state(St, StateMgr);
823
824 // First, retrieve the region view of the base region.
825 RegionViewMapTy::data_type* d = state.get<RegionViewMapTy>(Base);
826 RegionViewTy L = d ? *d : RVFactory.GetEmptyList();
827
828 // Now add View to the region view.
829 L = RVFactory.Add(View, L);
830
831 // Create a new state with the new region view.
832 return state.set<RegionViewMapTy>(Base, L);
833}