blob: 95bb6f1a054432ad4405cc730a692579a255b02e [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);
Ted Kremenekf936f452009-05-04 06:18:28 +000082 SVal getLValueElement(const GRState* St, QualType elementType,
83 SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000084
Ted Kremenek9deb0e32008-10-24 20:32:16 +000085 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
86 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000087 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000088
Ted Kremenek9deb0e32008-10-24 20:32:16 +000089 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
90 /// 'this' object (C++). When used when analyzing a normal function this
91 /// method returns NULL.
Ted Kremenek39fc7152009-03-05 16:41:21 +000092 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000093
Ted Kremenek2ed14be2008-12-05 00:47:52 +000094 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
95 /// It returns a new Store with these values removed, and populates LSymbols
96 /// and DSymbols with the known set of live and dead symbols respectively.
Ted Kremenek241677a2009-01-21 22:26:05 +000097 Store
98 RemoveDeadBindings(const GRState* state, Stmt* Loc,
99 SymbolReaper& SymReaper,
100 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000101
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000102 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000103
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000104 const GRState* BindDecl(const GRState* St, const VarDecl* VD, SVal InitVal) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000105 Store store = BindDeclInternal(St->getStore(), VD, &InitVal);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000106 return StateMgr.MakeStateWithStore(St, store);
107 }
108
109 const GRState* BindDeclWithNoInit(const GRState* St, const VarDecl* VD) {
Ted Kremenek39fc7152009-03-05 16:41:21 +0000110 Store store = BindDeclInternal(St->getStore(), VD, 0);
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000111 return StateMgr.MakeStateWithStore(St, store);
112 }
113
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000114 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000115
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000116 static inline BindingsTy GetBindings(Store store) {
117 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000118 }
119
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000120 void print(Store store, std::ostream& Out, const char* nl, const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000121
122private:
123 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000124};
Ted Kremenek9e240492008-10-04 05:50:14 +0000125
Ted Kremenek4323a572008-07-10 22:03:41 +0000126} // end anonymous namespace
127
128
Ted Kremenek5f81c442008-08-28 23:31:31 +0000129StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
130 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000131}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000132
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000133SVal BasicStoreManager::getLValueVar(const GRState* St, const VarDecl* VD) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000134 return Loc::MakeVal(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000135}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000136
137SVal BasicStoreManager::getLValueString(const GRState* St,
138 const StringLiteral* S) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000139 return Loc::MakeVal(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000140}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000141
142SVal BasicStoreManager::getLValueCompoundLiteral(const GRState* St,
143 const CompoundLiteralExpr* CL){
Zhongxing Xu56a34602008-12-21 03:31:01 +0000144 return Loc::MakeVal(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000145}
146
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000147SVal BasicStoreManager::getLValueIvar(const GRState* St, const ObjCIvarDecl* D,
148 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000149
150 if (Base.isUnknownOrUndef())
151 return Base;
152
153 Loc BaseL = cast<Loc>(Base);
154
155 if (isa<loc::MemRegionVal>(BaseL)) {
156 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
157
158 if (BaseR == SelfRegion)
159 return loc::MemRegionVal(MRMgr.getObjCIvarRegion(D, BaseR));
160 }
161
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000162 return UnknownVal();
163}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000164
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000165SVal BasicStoreManager::getLValueField(const GRState* St, SVal Base,
166 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000167
168 if (Base.isUnknownOrUndef())
169 return Base;
170
171 Loc BaseL = cast<Loc>(Base);
172 const MemRegion* BaseR = 0;
173
174 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000175 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000176 return UndefinedVal();
177
178 case loc::MemRegionKind:
179 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
180 break;
181
182 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000183 // While these seem funny, this can happen through casts.
184 // FIXME: What we should return is the field offset. For example,
185 // add the field offset to the integer value. That way funny things
186 // like this work properly: &(((struct foo *) 0xa)->f)
187 return Base;
188
189 default:
190 assert ("Unhandled Base.");
191 return Base;
192 }
193
Zhongxing Xu56a34602008-12-21 03:31:01 +0000194 return Loc::MakeVal(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000195}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000196
Ted Kremenekf936f452009-05-04 06:18:28 +0000197SVal BasicStoreManager::getLValueElement(const GRState* St,
198 QualType elementType,
199 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000200
Ted Kremenek7d71b292008-12-09 21:20:27 +0000201 if (Base.isUnknownOrUndef())
202 return Base;
203
204 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000205 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000206
207 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000208 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000209 // Technically we can get here if people do funny things with casts.
210 return UndefinedVal();
211
Ted Kremenekabb042f2008-12-13 19:24:37 +0000212 case loc::MemRegionKind: {
213 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000214
Ted Kremenekc2eeb722009-05-04 15:25:46 +0000215 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000216 // int x;
217 // char* y = (char*) &x;
218 // 'y' => ElementRegion(0, VarRegion('x'))
219 // y[0] = 'a';
Ted Kremenekc2eeb722009-05-04 15:25:46 +0000220 assert(ER->getIndex().isUnknown() ||
221 cast<nonloc::ConcreteInt>(ER->getIndex()).getValue() == 0);
222 ER = ER; // silence 'unused' warning in release modes.
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000223 return Base;
224 }
225
226
Ted Kremenekabb042f2008-12-13 19:24:37 +0000227 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
228 BaseR = TR;
229 break;
230 }
231
Zhongxing Xua1718c72009-04-03 07:33:13 +0000232 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
233 SymbolRef Sym = SR->getSymbol();
234 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
235 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000236
Ted Kremenek7d71b292008-12-09 21:20:27 +0000237 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000238 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000239
Ted Kremenek7d71b292008-12-09 21:20:27 +0000240 case loc::ConcreteIntKind:
241 // While these seem funny, this can happen through casts.
242 // FIXME: What we should return is the field offset. For example,
243 // add the field offset to the integer value. That way funny things
244 // like this work properly: &(((struct foo *) 0xa)->f)
245 return Base;
246
247 default:
248 assert ("Unhandled Base.");
249 return Base;
250 }
251
Ted Kremenekabb042f2008-12-13 19:24:37 +0000252 if (BaseR)
Ted Kremenekf936f452009-05-04 06:18:28 +0000253 return Loc::MakeVal(MRMgr.getElementRegion(elementType, UnknownVal(),
254 BaseR));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000255 else
256 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000257}
258
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000259static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000260 bool foundPointer = false;
261 while (1) {
262 const PointerType *PT = T->getAsPointerType();
263 if (!PT) {
264 if (!foundPointer)
265 return false;
266
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000267 // intptr_t* or intptr_t**, etc?
268 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
269 return true;
270
Ted Kremenek1670e402009-04-11 00:11:10 +0000271 QualType X = C.getCanonicalType(T).getUnqualifiedType();
272 return X == C.VoidTy;
273 }
274
275 foundPointer = true;
276 T = PT->getPointeeType();
277 }
278}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000279
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000280SVal BasicStoreManager::Retrieve(const GRState* state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000281
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000282 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000283 return UnknownVal();
284
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000285 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000286
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000287 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000288
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000289 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000290 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000291
Ted Kremenek20bd7462009-05-04 07:04:36 +0000292 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000293 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
294 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000295 ASTContext &Ctx = StateMgr.getContext();
Ted Kremenek20bd7462009-05-04 07:04:36 +0000296 QualType T = ER->getLValueType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000297
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000298 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek1670e402009-04-11 00:11:10 +0000299 return UnknownVal();
300
Ted Kremenek20bd7462009-05-04 07:04:36 +0000301 // FIXME: Should check for element 0.
302 // Otherwise, strip the element region.
303 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000304 }
305
Ted Kremenekf684d562009-03-05 18:08:28 +0000306 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000307 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000308
Ted Kremenekf684d562009-03-05 18:08:28 +0000309 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000310 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000311 return T ? *T : UnknownVal();
312 }
313
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000314 case loc::ConcreteIntKind:
315 // Some clients may call GetSVal with such an option simply because
316 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000317 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000318 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000319
Ted Kremenek4323a572008-07-10 22:03:41 +0000320 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000321 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000322 break;
323 }
324
325 return UnknownVal();
326}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000327
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000328Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
329 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000330 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000331 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenekd91719a2009-05-01 19:04:28 +0000332 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000333
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000334 // Special case: handle store of pointer values (Loc) to pointers via
335 // a cast to intXX_t*, void*, etc. This is needed to handle
336 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
337 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
Ted Kremenek20bd7462009-05-04 07:04:36 +0000338 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
339 // FIXME: Should check for index 0.
340 QualType T = ER->getLValueType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000341
Ted Kremenekd91719a2009-05-01 19:04:28 +0000342 if (isHigherOrderRawPtr(T, C))
Ted Kremenek20bd7462009-05-04 07:04:36 +0000343 R = ER->getSuperRegion();
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000344 }
345
Ted Kremenekf684d562009-03-05 18:08:28 +0000346 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000347 return store;
348
Ted Kremenekf684d562009-03-05 18:08:28 +0000349 // We only track bindings to self.ivar.
350 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
351 if (IVR->getSuperRegion() != SelfRegion)
352 return store;
Ted Kremenekd91719a2009-05-01 19:04:28 +0000353
354 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
355 // Only convert 'V' to a location iff the underlying region type
356 // is a location as well.
357 // FIXME: We are allowing a store of an arbitrary location to
358 // a pointer. We may wish to flag a type error here if the types
359 // are incompatible. This may also cause lots of breakage
360 // elsewhere. Food for thought.
361 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
362 if (TyR->isBoundable(C) &&
363 Loc::IsLocType(TyR->getRValueType(C)))
364 V = X->getLoc();
365 }
366 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000367
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000368 BindingsTy B = GetBindings(store);
Ted Kremenek4323a572008-07-10 22:03:41 +0000369 return V.isUnknown()
Ted Kremenekbe912242009-03-05 16:31:07 +0000370 ? VBFactory.Remove(B, R).getRoot()
371 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000372 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000373 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000374 assert ("SetSVal for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000375 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000376 }
377}
378
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000379Store BasicStoreManager::Remove(Store store, Loc loc) {
380 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000381 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000382 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000383
Ted Kremenekf684d562009-03-05 18:08:28 +0000384 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000385 return store;
386
Ted Kremenekf684d562009-03-05 18:08:28 +0000387 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000388 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000389 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000390 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000391 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000392 }
393}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000394
Ted Kremenek9e240492008-10-04 05:50:14 +0000395Store
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000396BasicStoreManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000397 SymbolReaper& SymReaper,
398 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
399{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000400
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000401 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000402 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000403 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000404
405 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000406 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000407 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
408 if (SymReaper.isLive(Loc, VR->getDecl()))
409 RegionRoots.push_back(VR);
410 else
411 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000412 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000413 else if (isa<ObjCIvarRegion>(I.getKey())) {
414 RegionRoots.push_back(I.getKey());
415 }
416 else
417 continue;
418
419 // Mark the bindings in the data as live.
420 SVal X = I.getData();
421 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
422 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000423 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000424
425 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000426 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000427
Ted Kremenek9e240492008-10-04 05:50:14 +0000428 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000429 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000430 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000431
Ted Kremenek134e7492008-10-17 22:52:40 +0000432 while (MR) {
433 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000434 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000435 break;
436 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000437 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
438 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000439 break;
440
Ted Kremenekf684d562009-03-05 18:08:28 +0000441 Marked.insert(MR);
442 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000443
Ted Kremenek134e7492008-10-17 22:52:40 +0000444 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000445 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
446 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000447
Ted Kremenek134e7492008-10-17 22:52:40 +0000448 if (!isa<loc::MemRegionVal>(X))
449 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000450
Ted Kremenek134e7492008-10-17 22:52:40 +0000451 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
452 RegionRoots.push_back(LVD.getRegion());
453 break;
454 }
455 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
456 MR = R->getSuperRegion();
457 else
458 break;
459 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000460 }
461
462 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000463 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000464 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000465
466 if (!Marked.count(R)) {
Zhongxing Xu56a34602008-12-21 03:31:01 +0000467 store = Remove(store, Loc::MakeVal(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000468 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000469
470 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000471 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000472 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000473 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000474
Ted Kremenekf59bf482008-07-17 18:38:48 +0000475 return store;
476}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000477
Ted Kremenekf684d562009-03-05 18:08:28 +0000478Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
479 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
480 CI != CE; ++CI) {
481
482 if (!*CI)
483 continue;
484
485 // Check if the statement is an ivar reference. We only
486 // care about self.ivar.
487 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
488 const Expr *Base = IV->getBase()->IgnoreParenCasts();
489 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
490 if (DR->getDecl() == SelfDecl) {
491 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000492 SelfRegion);
493 SVal X = ValMgr.getRValueSymbolVal(IVR);
Ted Kremenekf684d562009-03-05 18:08:28 +0000494 St = BindInternal(St, Loc::MakeVal(IVR), X);
495 }
496 }
497 }
498 else
499 St = scanForIvars(*CI, SelfDecl, St);
500 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000501
Ted Kremenekf684d562009-03-05 18:08:28 +0000502 return St;
503}
504
505Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000506 // The LiveVariables information already has a compilation of all VarDecls
507 // used in the function. Iterate through this set, and "symbolicate"
508 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000509 typedef LiveVariables::AnalysisDataTy LVDataTy;
510 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000511 Store St = VBFactory.GetEmptyMap().getRoot();
512
513 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000514 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000515
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000516 // Handle implicit parameters.
517 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
518 const Decl& CD = StateMgr.getCodeDecl();
519 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
520 if (MD->getSelfDecl() == PD) {
521 // Create a region for "self".
522 assert (SelfRegion == 0);
523 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
524 MRMgr.getHeapRegion());
525
Zhongxing Xu56a34602008-12-21 03:31:01 +0000526 St = BindInternal(St, Loc::MakeVal(MRMgr.getVarRegion(PD)),
527 Loc::MakeVal(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000528
529 // Scan the method for ivar references. While this requires an
530 // entire AST scan, the cost should not be high in practice.
Douglas Gregor72971342009-04-18 00:02:19 +0000531 St = scanForIvars(MD->getBody(getContext()), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000532 }
533 }
534 }
535 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000536 // Punt on static variables for now.
537 if (VD->getStorageClass() == VarDecl::Static)
538 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000539
540 // Only handle simple types that we can symbolicate.
541 if (!SymbolManager::canSymbolicate(VD->getType()))
542 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000543
Ted Kremenekec099f12009-03-18 22:10:22 +0000544 // Initialize globals and parameters to symbolic values.
545 // Initialize local variables to undefined.
546 const MemRegion *R = StateMgr.getRegion(VD);
547 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
548 isa<ImplicitParamDecl>(VD))
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000549 ? ValMgr.getRValueSymbolVal(R)
Ted Kremenekec099f12009-03-18 22:10:22 +0000550 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000551
Ted Kremenekec099f12009-03-18 22:10:22 +0000552 St = BindInternal(St, Loc::MakeVal(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000553 }
554 }
555 return St;
556}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000557
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000558Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
559 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000560
Ted Kremeneke53c0692008-08-23 00:50:55 +0000561 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000562
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000563 // BasicStore does not model arrays and structs.
564 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
565 return store;
566
567 if (VD->hasGlobalStorage()) {
568 // Handle variables with global storage: extern, static, PrivateExtern.
569
570 // FIXME:: static variables may have an initializer, but the second time a
571 // function is called those values may not be current. Currently, a function
572 // will not be called more than once.
573
574 // Static global variables should not be visited here.
575 assert(!(VD->getStorageClass() == VarDecl::Static &&
576 VD->isFileVarDecl()));
577
578 // Process static variables.
579 if (VD->getStorageClass() == VarDecl::Static) {
580 // C99: 6.7.8 Initialization
581 // If an object that has static storage duration is not initialized
582 // explicitly, then:
583 // —if it has pointer type, it is initialized to a null pointer;
584 // —if it has arithmetic type, it is initialized to (positive or
585 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000586 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000587 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000588 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000589 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000590 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000591 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000592 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000593 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000594 else {
595 // assert(0 && "ignore other types of variables");
596 }
597 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000598 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000599 }
600 }
601 } else {
602 // Process local scalar variables.
603 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000604 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000605 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000606 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000607 }
608 }
609
610 return store;
611}
612
Ted Kremenekbe912242009-03-05 16:31:07 +0000613void BasicStoreManager::print(Store store, std::ostream& O,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000614 const char* nl, const char *sep) {
615
Ted Kremenekbe912242009-03-05 16:31:07 +0000616 llvm::raw_os_ostream Out(O);
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000617 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000618 Out << "Variables:" << nl;
619
620 bool isFirst = true;
621
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000622 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000623 if (isFirst) isFirst = false;
624 else Out << nl;
625
Ted Kremenekbe912242009-03-05 16:31:07 +0000626 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000627 I.getData().print(Out);
628 }
629}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000630
Ted Kremenek60dbad82008-09-03 03:06:11 +0000631
632void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000633 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000634
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000635 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000636 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000637
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000638}
639
Ted Kremenek60dbad82008-09-03 03:06:11 +0000640StoreManager::BindingsHandler::~BindingsHandler() {}