blob: 616c4b51350ab9ce7e8dcacc09b68fa7610ed5cb [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 Kremenek1670e402009-04-11 00:11:10 +0000254static bool isHigherOrderVoidPtr(QualType T, ASTContext &C) {
255 bool foundPointer = false;
256 while (1) {
257 const PointerType *PT = T->getAsPointerType();
258 if (!PT) {
259 if (!foundPointer)
260 return false;
261
262 QualType X = C.getCanonicalType(T).getUnqualifiedType();
263 return X == C.VoidTy;
264 }
265
266 foundPointer = true;
267 T = PT->getPointeeType();
268 }
269}
270
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000271SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000272
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000273 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000274 return UnknownVal();
275
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000276 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000277
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000278 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000279
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000280 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000281 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000282
Ted Kremenek1670e402009-04-11 00:11:10 +0000283 if (const TypedViewRegion *TR = dyn_cast<TypedViewRegion>(R)) {
284 // Just support void**, void***, etc., for now. This is needed
285 // to handle OSCompareAndSwapPtr().
286 ASTContext &Ctx = StateMgr.getContext();
287 QualType T = TR->getLValueType(Ctx);
288
289 if (!isHigherOrderVoidPtr(T, Ctx))
290 return UnknownVal();
291
292 // Otherwise, strip the views.
293 // FIXME: Should we layer a TypedView on the result?
294 R = TR->removeViews();
295 }
296
Ted Kremenekf684d562009-03-05 18:08:28 +0000297 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000298 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000299
Ted Kremenekf684d562009-03-05 18:08:28 +0000300 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000301 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000302 return T ? *T : UnknownVal();
303 }
304
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000305 case loc::ConcreteIntKind:
306 // Some clients may call GetSVal with such an option simply because
307 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000308 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000309 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000310
Ted Kremenek4323a572008-07-10 22:03:41 +0000311 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000312 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000313 break;
314 }
315
316 return UnknownVal();
317}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000318
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000319Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
320 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000321 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000322 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000323
Ted Kremenekf684d562009-03-05 18:08:28 +0000324 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000325 return store;
326
Ted Kremenekf684d562009-03-05 18:08:28 +0000327 // We only track bindings to self.ivar.
328 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
329 if (IVR->getSuperRegion() != SelfRegion)
330 return store;
331
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000332 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000333 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000334 ? VBFactory.Remove(B, R).getRoot()
335 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000336 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000337 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000338 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000339 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000340 }
341}
342
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000343Store BasicStoreManager::Remove(Store store, Loc loc) {
344 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000345 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000346 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000347
Ted Kremenekf684d562009-03-05 18:08:28 +0000348 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000349 return store;
350
Ted Kremenekf684d562009-03-05 18:08:28 +0000351 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000352 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000353 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000354 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000355 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000356 }
357}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000358
Ted Kremenek9e240492008-10-04 05:50:14 +0000359Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000360BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000361 SymbolReaper& SymReaper,
362 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
363{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000364
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000365 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000366 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000367 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000368
369 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000370 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000371 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
372 if (SymReaper.isLive(Loc, VR->getDecl()))
373 RegionRoots.push_back(VR);
374 else
375 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000376 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000377 else if (isa<ObjCIvarRegion>(I.getKey())) {
378 RegionRoots.push_back(I.getKey());
379 }
380 else
381 continue;
382
383 // Mark the bindings in the data as live.
384 SVal X = I.getData();
385 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
386 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000387 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000388
389 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000390 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000391
Ted Kremenek9e240492008-10-04 05:50:14 +0000392 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000393 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000394 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000395
Ted Kremenek134e7492008-10-17 22:52:40 +0000396 while (MR) {
397 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000398 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000399 break;
400 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000401 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
402 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000403 break;
404
Ted Kremenekf684d562009-03-05 18:08:28 +0000405 Marked.insert(MR);
406 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000407
Ted Kremenek134e7492008-10-17 22:52:40 +0000408 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000409 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
410 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000411
Ted Kremenek134e7492008-10-17 22:52:40 +0000412 if (!isa<loc::MemRegionVal>(X))
413 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000414
Ted Kremenek134e7492008-10-17 22:52:40 +0000415 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
416 RegionRoots.push_back(LVD.getRegion());
417 break;
418 }
419 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
420 MR = R->getSuperRegion();
421 else
422 break;
423 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000424 }
425
426 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000427 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000428 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000429
430 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000431 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000432 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000433
434 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000435 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000436 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000437 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000438
Ted Kremenekf59bf482008-07-17 18:38:48 +0000439 return store;
440}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000441
Ted Kremenekf684d562009-03-05 18:08:28 +0000442Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
443 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
444 CI != CE; ++CI) {
445
446 if (!*CI)
447 continue;
448
449 // Check if the statement is an ivar reference. We only
450 // care about self.ivar.
451 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
452 const Expr *Base = IV->getBase()->IgnoreParenCasts();
453 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
454 if (DR->getDecl() == SelfDecl) {
455 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000456 SelfRegion);
457 SVal X = ValMgr.getRValueSymbolVal(IVR);
Ted Kremenekf684d562009-03-05 18:08:28 +0000458 St = BindInternal(St, Loc::MakeVal(IVR), X);
459 }
460 }
461 }
462 else
463 St = scanForIvars(*CI, SelfDecl, St);
464 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000465
Ted Kremenekf684d562009-03-05 18:08:28 +0000466 return St;
467}
468
469Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000470 // The LiveVariables information already has a compilation of all VarDecls
471 // used in the function. Iterate through this set, and "symbolicate"
472 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000473 typedef LiveVariables::AnalysisDataTy LVDataTy;
474 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000475 Store St = VBFactory.GetEmptyMap().getRoot();
476
477 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000478 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000479
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000480 // Handle implicit parameters.
481 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
482 const Decl& CD = StateMgr.getCodeDecl();
483 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
484 if (MD->getSelfDecl() == PD) {
485 // Create a region for "self".
486 assert (SelfRegion == 0);
487 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
488 MRMgr.getHeapRegion());
489
Zhongxing Xu56a34602008-12-21 03:31:01 +0000490 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
491 Loc::MakeVal(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000492
493 // Scan the method for ivar references. While this requires an
494 // entire AST scan, the cost should not be high in practice.
Douglas Gregor72971342009-04-18 00:02:19 +0000495 St = scanForIvars(MD->getBody(getContext()), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000496 }
497 }
498 }
499 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000500 // Punt on static variables for now.
501 if (VD->getStorageClass() == VarDecl::Static)
502 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000503
504 // Only handle simple types that we can symbolicate.
505 if (!SymbolManager::canSymbolicate(VD->getType()))
506 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000507
Ted Kremenekec099f12009-03-18 22:10:22 +0000508 // Initialize globals and parameters to symbolic values.
509 // Initialize local variables to undefined.
510 const MemRegion *R = StateMgr.getRegion(VD);
511 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
512 isa<ImplicitParamDecl>(VD))
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000513 ? ValMgr.getRValueSymbolVal(R)
Ted Kremenekec099f12009-03-18 22:10:22 +0000514 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000515
Ted Kremenekec099f12009-03-18 22:10:22 +0000516 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000517 }
518 }
519 return St;
520}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000521
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000522Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
523 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000524
Ted Kremeneke53c0692008-08-23 00:50:55 +0000525 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000526
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000527 // BasicStore does not model arrays and structs.
528 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
529 return store;
530
531 if (VD->hasGlobalStorage()) {
532 // Handle variables with global storage: extern, static, PrivateExtern.
533
534 // FIXME:: static variables may have an initializer, but the second time a
535 // function is called those values may not be current. Currently, a function
536 // will not be called more than once.
537
538 // Static global variables should not be visited here.
539 assert(!(VD->getStorageClass() == VarDecl::Static &&
540 VD->isFileVarDecl()));
541
542 // Process static variables.
543 if (VD->getStorageClass() == VarDecl::Static) {
544 // C99: 6.7.8 Initialization
545 // If an object that has static storage duration is not initialized
546 // explicitly, then:
547 // —if it has pointer type, it is initialized to a null pointer;
548 // —if it has arithmetic type, it is initialized to (positive or
549 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000550 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000551 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000552 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000553 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000554 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000555 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000556 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000557 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000558 else {
559 // assert(0 && "ignore other types of variables");
560 }
561 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000562 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000563 }
564 }
565 } else {
566 // Process local scalar variables.
567 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000568 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000569 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000570 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000571 }
572 }
573
574 return store;
575}
576
Ted Kremenekbe912242009-03-05 16:31:07 +0000577void BasicStoreManager::print(Store store, std::ostream& O,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000578 const char* nl, const char *sep) {
579
Ted Kremenekbe912242009-03-05 16:31:07 +0000580 llvm::raw_os_ostream Out(O);
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000581 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000582 Out << "Variables:" << nl;
583
584 bool isFirst = true;
585
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000586 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000587 if (isFirst) isFirst = false;
588 else Out << nl;
589
Ted Kremenekbe912242009-03-05 16:31:07 +0000590 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000591 I.getData().print(Out);
592 }
593}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000594
Ted Kremenek60dbad82008-09-03 03:06:11 +0000595
596void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000597 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000598
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000599 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000600 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000601
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000602}
603
Ted Kremenek60dbad82008-09-03 03:06:11 +0000604StoreManager::BindingsHandler::~BindingsHandler() {}