blob: c9bf9ea566275a024fef342dfcf3254f86355dfc [file] [log] [blame]
Ted Kremenek4323a572008-07-10 22:03:41 +00001//== BasicStore.cpp - Basic map from Locations to Values --------*- 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 defined the BasicStore and BasicStoreManager classes.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekf684d562009-03-05 18:08:28 +000014#include "clang/AST/ExprObjC.h"
Ted Kremenek5f81c442008-08-28 23:31:31 +000015#include "clang/Analysis/Analyses/LiveVariables.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000016#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000017#include "llvm/ADT/ImmutableMap.h"
18#include "llvm/Support/Compiler.h"
Ted Kremeneka622d8c2008-08-19 22:24:03 +000019#include "llvm/Support/Streams.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000020
21using namespace clang;
22
Ted Kremenek2c8e7882009-03-05 16:32:59 +000023typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000024
Ted Kremenek4323a572008-07-10 22:03:41 +000025namespace {
26
Ted Kremenek59e8f112009-03-03 01:35:36 +000027class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
28public:
29 BasicStoreSubRegionMap() {}
30
Ted Kremenek5dc27462009-03-03 02:51:43 +000031 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek39fc7152009-03-05 16:41:21 +000032 return true; // Do nothing. No subregions.
Ted Kremenek59e8f112009-03-03 01:35:36 +000033 }
34};
35
Ted Kremenek4323a572008-07-10 22:03:41 +000036class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek2c8e7882009-03-05 16:32:59 +000037 BindingsTy::Factory VBFactory;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000038 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000039
40public:
Zhongxing Xubc678fd2008-10-07 01:31:04 +000041 BasicStoreManager(GRStateManager& mgr)
Ted Kremenekc62abc12009-04-21 21:51:34 +000042 : StoreManager(mgr),
Ted Kremenekd6cfbe42009-01-07 22:18:50 +000043 VBFactory(mgr.getAllocator()),
Zhongxing Xu0adfbf62008-11-15 08:19:58 +000044 SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000045
Ted Kremenek9deb0e32008-10-24 20:32:16 +000046 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000047
Ted Kremenek14453bf2009-03-03 19:02:42 +000048 SubRegionMap* getSubRegionMap(const GRState *state) {
49 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000050 }
51
Ted Kremenekc6ed3842009-01-07 22:56:17 +000052 SVal Retrieve(const GRState *state, Loc loc, QualType T = QualType());
Zhongxing Xu4193eca2008-12-20 06:32:12 +000053
54 const GRState* Bind(const GRState* St, Loc L, SVal V) {
Ted Kremenek39fc7152009-03-05 16:41:21 +000055 Store store = BindInternal(St->getStore(), L, V);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000056 return StateMgr.MakeStateWithStore(St, store);
57 }
58
Ted Kremenekf684d562009-03-05 18:08:28 +000059 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
60
Ted Kremenekc6ed3842009-01-07 22:56:17 +000061 Store BindInternal(Store St, Loc loc, SVal V);
62 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000063 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000064
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000065 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000066 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +000067 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000068 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000069
Zhongxing Xu4193eca2008-12-20 06:32:12 +000070 const GRState* BindCompoundLiteral(const GRState* St,
71 const CompoundLiteralExpr* CL,
72 SVal V) {
73 return St;
Ted Kremenek4f090272008-10-27 21:54:31 +000074 }
75
Zhongxing Xu1c96b242008-10-17 05:57:07 +000076 SVal getLValueVar(const GRState* St, const VarDecl* VD);
Zhongxing Xu143bf822008-10-25 14:18:57 +000077 SVal getLValueString(const GRState* St, const StringLiteral* S);
Zhongxing Xuf22679e2008-11-07 10:38:33 +000078 SVal getLValueCompoundLiteral(const GRState* St,
79 const CompoundLiteralExpr* CL);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000080 SVal getLValueIvar(const GRState* St, const ObjCIvarDecl* D, SVal Base);
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +000081 SVal getLValueField(const GRState* St, SVal Base, const FieldDecl* D);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000082 SVal getLValueElement(const GRState* St, SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000083
Ted Kremenek9deb0e32008-10-24 20:32:16 +000084 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
85 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000086 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000087
Ted Kremenek9deb0e32008-10-24 20:32:16 +000088 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
89 /// 'this' object (C++). When used when analyzing a normal function this
90 /// method returns NULL.
Ted Kremenek39fc7152009-03-05 16:41:21 +000091 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000092
Ted Kremenek2ed14be2008-12-05 00:47:52 +000093 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
94 /// It returns a new Store with these values removed, and populates LSymbols
95 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +000096 Store
97 RemoveDeadBindings(const GRState* state, Stmt* Loc,
98 SymbolReaper& SymReaper,
99 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000100
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000101 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000102
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000103 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000104 Store store = BindDeclInternal(St->getStore(), VD, &InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000105 return StateMgr.MakeStateWithStore(St, store);
106 }
107
108 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000109 Store store = BindDeclInternal(St->getStore(), VD, 0);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000110 return StateMgr.MakeStateWithStore(St, store);
111 }
112
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000113 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000114
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000115 static inline BindingsTy GetBindings(Store store) {
116 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000117 }
118
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000119 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000120
121private:
122 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000123};
Ted Kremenek9e240492008-10-04 05:50:14 +0000124
Ted Kremenek4323a572008-07-10 22:03:41 +0000125} // end anonymous namespace
126
127
Ted Kremenek5f81c442008-08-28 23:31:31 +0000128StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
129 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000130}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000131
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000132SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000133 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000134}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000135
136SVal BasicStoreManager::getLValueString(const GRState* St,
137 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000138 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000139}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000140
141SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
142 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000143 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000144}
145
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000146SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
147 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000148
149 if (Base.isUnknownOrUndef())
150 return Base;
151
152 Loc BaseL = cast<Loc>(Base);
153
154 if (isa<loc::MemRegionVal>(BaseL)) {
155 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
156
157 if (BaseR == SelfRegion)
158 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(D, BaseR));
159 }
160
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000161 return UnknownVal();
162}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000163
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000164SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
165 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000166
167 if (Base.isUnknownOrUndef())
168 return Base;
169
170 Loc BaseL = cast<Loc>(Base);
171 const MemRegion* BaseR = 0;
172
173 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000174 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000175 return UndefinedVal();
176
177 case loc::MemRegionKind:
178 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
179 break;
180
181 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000182 // While these seem funny, this can happen through casts.
183 // FIXME: What we should return is the field offset. For example,
184 // add the field offset to the integer value. That way funny things
185 // like this work properly: &(((struct foo *) 0xa)->f)
186 return Base;
187
188 default:
189 assert ("Unhandled Base.");
190 return Base;
191 }
192
Zhongxing Xu56a34602008-12-21 03:31:01 +0000193 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000194}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000195
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000196SVal BasicStoreManager::getLValueElement(const GRState* St, SVal Base,
197 SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000198
Ted Kremenek7d71b292008-12-09 21:20:27 +0000199 if (Base.isUnknownOrUndef())
200 return Base;
201
202 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000203 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204
205 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000206 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000207 // Technically we can get here if people do funny things with casts.
208 return UndefinedVal();
209
Ted Kremenekabb042f2008-12-13 19:24:37 +0000210 case loc::MemRegionKind: {
211 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000212
213 if (isa<ElementRegion>(R)) {
214 // Basic example:
215 // char buf[100];
216 // char *q = &buf[1]; // p points to ElementRegion(buf,Unknown)
217 // &q[10]
218 assert(cast<ElementRegion>(R)->getIndex().isUnknown());
219 return Base;
220 }
221
222
Ted Kremenekabb042f2008-12-13 19:24:37 +0000223 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
224 BaseR = TR;
225 break;
226 }
227
Zhongxing Xua1718c72009-04-03 07:33:13 +0000228 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
229 SymbolRef Sym = SR->getSymbol();
230 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
231 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000232
Ted Kremenek7d71b292008-12-09 21:20:27 +0000233 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000234 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000235
Ted Kremenek7d71b292008-12-09 21:20:27 +0000236 case loc::ConcreteIntKind:
237 // While these seem funny, this can happen through casts.
238 // FIXME: What we should return is the field offset. For example,
239 // add the field offset to the integer value. That way funny things
240 // like this work properly: &(((struct foo *) 0xa)->f)
241 return Base;
242
243 default:
244 assert ("Unhandled Base.");
245 return Base;
246 }
247
Ted Kremenekabb042f2008-12-13 19:24:37 +0000248 if (BaseR)
Zhongxing Xu56a34602008-12-21 03:31:01 +0000249 return Loc::MakeVal(MRMgr.getElementRegion(UnknownVal(), BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000250 else
251 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000252}
253
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000254static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000255 bool foundPointer = false;
256 while (1) {
257 const PointerType *PT = T->getAsPointerType();
258 if (!PT) {
259 if (!foundPointer)
260 return false;
261
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000262 // intptr_t* or intptr_t**, etc?
263 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
264 return true;
265
Ted Kremenek1670e402009-04-11 00:11:10 +0000266 QualType X = C.getCanonicalType(T).getUnqualifiedType();
267 return X == C.VoidTy;
268 }
269
270 foundPointer = true;
271 T = PT->getPointeeType();
272 }
273}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000274
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000275SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000276
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000277 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000278 return UnknownVal();
279
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000280 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000281
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000282 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000283
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000284 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000285 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000286
Ted Kremenek1670e402009-04-11 00:11:10 +0000287 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000288 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
289 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000290 ASTContext &Ctx = StateMgr.getContext();
291 QualType T = TR->getLValueType(Ctx);
292
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000293 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek1670e402009-04-11 00:11:10 +0000294 return UnknownVal();
295
296 // Otherwise, strip the views.
297 // FIXME: Should we layer a TypedView on the result?
298 R = TR->removeViews();
299 }
300
Ted Kremenekf684d562009-03-05 18:08:28 +0000301 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000302 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000303
Ted Kremenekf684d562009-03-05 18:08:28 +0000304 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000305 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000306 return T ? *T : UnknownVal();
307 }
308
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000309 case loc::ConcreteIntKind:
310 // Some clients may call GetSVal with such an option simply because
311 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000312 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000313 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000314
Ted Kremenek4323a572008-07-10 22:03:41 +0000315 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000316 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000317 break;
318 }
319
320 return UnknownVal();
321}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000322
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000323Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
324 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000325 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000326 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000327
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000328 // Special case: handle store of pointer values (Loc) to pointers via
329 // a cast to intXX_t*, void*, etc. This is needed to handle
330 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
331 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
332 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
333 ASTContext &C = StateMgr.getContext();
334 QualType T = TR->getLValueType(C);
335
336 if (isHigherOrderRawPtr(T, C)) {
337 R = TR->removeViews();
338
339 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
340 // Only convert 'V' to a location iff the underlying region type
341 // is a location as well.
342 // FIXME: We are allowing a store of an arbitrary location to
343 // a pointer. We may wish to flag a type error here if the types
344 // are incompatible. This may also cause lots of breakage
345 // elsewhere. Food for thought.
346 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
347 if (TyR->isBoundable(C) &&
348 Loc::IsLocType(TyR->getRValueType(C)))
349 V = X->getLoc();
350 }
351 }
352 }
353 }
354
Ted Kremenekf684d562009-03-05 18:08:28 +0000355 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000356 return store;
357
Ted Kremenekf684d562009-03-05 18:08:28 +0000358 // We only track bindings to self.ivar.
359 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
360 if (IVR->getSuperRegion() != SelfRegion)
361 return store;
362
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000363 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000364 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000365 ? VBFactory.Remove(B, R).getRoot()
366 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000367 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000368 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000369 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000370 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000371 }
372}
373
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000374Store BasicStoreManager::Remove(Store store, Loc loc) {
375 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000376 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000377 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000378
Ted Kremenekf684d562009-03-05 18:08:28 +0000379 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000380 return store;
381
Ted Kremenekf684d562009-03-05 18:08:28 +0000382 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000383 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000384 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000385 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000386 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000387 }
388}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000389
Ted Kremenek9e240492008-10-04 05:50:14 +0000390Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000391BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000392 SymbolReaper& SymReaper,
393 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
394{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000395
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000396 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000397 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000398 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000399
400 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000401 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000402 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
403 if (SymReaper.isLive(Loc, VR->getDecl()))
404 RegionRoots.push_back(VR);
405 else
406 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000407 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000408 else if (isa<ObjCIvarRegion>(I.getKey())) {
409 RegionRoots.push_back(I.getKey());
410 }
411 else
412 continue;
413
414 // Mark the bindings in the data as live.
415 SVal X = I.getData();
416 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
417 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000418 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000419
420 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000421 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000422
Ted Kremenek9e240492008-10-04 05:50:14 +0000423 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000424 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000425 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000426
Ted Kremenek134e7492008-10-17 22:52:40 +0000427 while (MR) {
428 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000429 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000430 break;
431 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000432 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
433 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000434 break;
435
Ted Kremenekf684d562009-03-05 18:08:28 +0000436 Marked.insert(MR);
437 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000438
Ted Kremenek134e7492008-10-17 22:52:40 +0000439 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000440 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
441 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000442
Ted Kremenek134e7492008-10-17 22:52:40 +0000443 if (!isa<loc::MemRegionVal>(X))
444 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000445
Ted Kremenek134e7492008-10-17 22:52:40 +0000446 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
447 RegionRoots.push_back(LVD.getRegion());
448 break;
449 }
450 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
451 MR = R->getSuperRegion();
452 else
453 break;
454 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000455 }
456
457 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000458 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000459 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000460
461 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000462 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000463 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000464
465 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000466 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000467 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000468 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000469
Ted Kremenekf59bf482008-07-17 18:38:48 +0000470 return store;
471}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000472
Ted Kremenekf684d562009-03-05 18:08:28 +0000473Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
474 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
475 CI != CE; ++CI) {
476
477 if (!*CI)
478 continue;
479
480 // Check if the statement is an ivar reference. We only
481 // care about self.ivar.
482 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
483 const Expr *Base = IV->getBase()->IgnoreParenCasts();
484 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
485 if (DR->getDecl() == SelfDecl) {
486 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000487 SelfRegion);
488 SVal X = ValMgr.getRValueSymbolVal(IVR);
Ted Kremenekf684d562009-03-05 18:08:28 +0000489 St = BindInternal(St, Loc::MakeVal(IVR), X);
490 }
491 }
492 }
493 else
494 St = scanForIvars(*CI, SelfDecl, St);
495 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000496
Ted Kremenekf684d562009-03-05 18:08:28 +0000497 return St;
498}
499
500Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000501 // The LiveVariables information already has a compilation of all VarDecls
502 // used in the function. Iterate through this set, and "symbolicate"
503 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000504 typedef LiveVariables::AnalysisDataTy LVDataTy;
505 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000506 Store St = VBFactory.GetEmptyMap().getRoot();
507
508 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000509 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000510
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000511 // Handle implicit parameters.
512 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
513 const Decl& CD = StateMgr.getCodeDecl();
514 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
515 if (MD->getSelfDecl() == PD) {
516 // Create a region for "self".
517 assert (SelfRegion == 0);
518 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
519 MRMgr.getHeapRegion());
520
Zhongxing Xu56a34602008-12-21 03:31:01 +0000521 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
522 Loc::MakeVal(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000523
524 // Scan the method for ivar references. While this requires an
525 // entire AST scan, the cost should not be high in practice.
Douglas Gregor72971342009-04-18 00:02:19 +0000526 St = scanForIvars(MD->getBody(getContext()), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000527 }
528 }
529 }
530 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000531 // Punt on static variables for now.
532 if (VD->getStorageClass() == VarDecl::Static)
533 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000534
535 // Only handle simple types that we can symbolicate.
536 if (!SymbolManager::canSymbolicate(VD->getType()))
537 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000538
Ted Kremenekec099f12009-03-18 22:10:22 +0000539 // Initialize globals and parameters to symbolic values.
540 // Initialize local variables to undefined.
541 const MemRegion *R = StateMgr.getRegion(VD);
542 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
543 isa<ImplicitParamDecl>(VD))
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000544 ? ValMgr.getRValueSymbolVal(R)
Ted Kremenekec099f12009-03-18 22:10:22 +0000545 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000546
Ted Kremenekec099f12009-03-18 22:10:22 +0000547 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000548 }
549 }
550 return St;
551}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000552
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000553Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
554 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000555
Ted Kremeneke53c0692008-08-23 00:50:55 +0000556 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000557
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000558 // BasicStore does not model arrays and structs.
559 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
560 return store;
561
562 if (VD->hasGlobalStorage()) {
563 // Handle variables with global storage: extern, static, PrivateExtern.
564
565 // FIXME:: static variables may have an initializer, but the second time a
566 // function is called those values may not be current. Currently, a function
567 // will not be called more than once.
568
569 // Static global variables should not be visited here.
570 assert(!(VD->getStorageClass() == VarDecl::Static &&
571 VD->isFileVarDecl()));
572
573 // Process static variables.
574 if (VD->getStorageClass() == VarDecl::Static) {
575 // C99: 6.7.8 Initialization
576 // If an object that has static storage duration is not initialized
577 // explicitly, then:
578 // —if it has pointer type, it is initialized to a null pointer;
579 // —if it has arithmetic type, it is initialized to (positive or
580 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000581 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000582 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000583 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000584 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000585 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000586 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000587 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000588 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000589 else {
590 // assert(0 && "ignore other types of variables");
591 }
592 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000593 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000594 }
595 }
596 } else {
597 // Process local scalar variables.
598 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000599 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000600 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000601 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000602 }
603 }
604
605 return store;
606}
607
Ted Kremenekbe912242009-03-05 16:31:07 +0000608void BasicStoreManager::print(Store store, std::ostream& O,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000609 const char* nl, const char *sep) {
610
Ted Kremenekbe912242009-03-05 16:31:07 +0000611 llvm::raw_os_ostream Out(O);
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000612 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000613 Out << "Variables:" << nl;
614
615 bool isFirst = true;
616
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000617 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000618 if (isFirst) isFirst = false;
619 else Out << nl;
620
Ted Kremenekbe912242009-03-05 16:31:07 +0000621 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000622 I.getData().print(Out);
623 }
624}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000625
Ted Kremenek60dbad82008-09-03 03:06:11 +0000626
627void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000628 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000629
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000630 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000631 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000632
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000633}
634
Ted Kremenek60dbad82008-09-03 03:06:11 +0000635StoreManager::BindingsHandler::~BindingsHandler() {}