blob: 7210d2c6794c593732f866a503921bf5c3993494 [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 Kremenek67f28532009-06-17 22:02:04 +000048 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek14453bf2009-03-03 19:02:42 +000049 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
Ted Kremenek67f28532009-06-17 22:02:04 +000054 const GRState *Bind(const GRState *state, Loc L, SVal V) {
55 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xu4193eca2008-12-20 06:32:12 +000056 }
57
Ted Kremenekf684d562009-03-05 18:08:28 +000058 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
59
Ted Kremenekc6ed3842009-01-07 22:56:17 +000060 Store BindInternal(Store St, Loc loc, SVal V);
61 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000062 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000063
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000064 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000065 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xud91ee272009-06-23 09:02:15 +000066 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000067 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000068
Ted Kremenek67f28532009-06-17 22:02:04 +000069 const GRState *BindCompoundLiteral(const GRState *state,
70 const CompoundLiteralExpr* cl,
71 SVal val) {
72 return state;
Ted Kremenek4f090272008-10-27 21:54:31 +000073 }
74
Ted Kremenek67f28532009-06-17 22:02:04 +000075 SVal getLValueVar(const GRState *state, const VarDecl* VD);
76 SVal getLValueString(const GRState *state, const StringLiteral* S);
77 SVal getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +000078 const CompoundLiteralExpr* CL);
Ted Kremenek67f28532009-06-17 22:02:04 +000079 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
80 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
81 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +000082 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.
Ted Kremenek67f28532009-06-17 22:02:04 +000094 /// It returns a new Store with these values removed.
95 Store RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +000096 SymbolReaper& SymReaper,
97 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000098
Ted Kremenek9deb0e32008-10-24 20:32:16 +000099 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000100
Ted Kremenek67f28532009-06-17 22:02:04 +0000101 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal) {
102 return state->makeWithStore(BindDeclInternal(state->getStore(),VD, &InitVal));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000103 }
104
Ted Kremenek67f28532009-06-17 22:02:04 +0000105 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
106 return state->makeWithStore(BindDeclInternal(state->getStore(), VD, 0));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000107 }
108
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000109 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000110
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000111 static inline BindingsTy GetBindings(Store store) {
112 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000113 }
114
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000115 void print(Store store, llvm::raw_ostream& Out, const char* nl,
116 const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000117
118private:
119 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000120};
Ted Kremenek9e240492008-10-04 05:50:14 +0000121
Ted Kremenek4323a572008-07-10 22:03:41 +0000122} // end anonymous namespace
123
124
Ted Kremenek5f81c442008-08-28 23:31:31 +0000125StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
126 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000127}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000128
Ted Kremenek67f28532009-06-17 22:02:04 +0000129SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000130 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000131}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000132
Ted Kremenek67f28532009-06-17 22:02:04 +0000133SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000134 const StringLiteral* S) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000135 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000136}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000137
Ted Kremenek67f28532009-06-17 22:02:04 +0000138SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000139 const CompoundLiteralExpr* CL){
Zhongxing Xud91ee272009-06-23 09:02:15 +0000140 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000141}
142
Ted Kremenek67f28532009-06-17 22:02:04 +0000143SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000144 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000145
146 if (Base.isUnknownOrUndef())
147 return Base;
148
149 Loc BaseL = cast<Loc>(Base);
150
151 if (isa<loc::MemRegionVal>(BaseL)) {
152 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
153
154 if (BaseR == SelfRegion)
Zhongxing Xud91ee272009-06-23 09:02:15 +0000155 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekf684d562009-03-05 18:08:28 +0000156 }
157
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000158 return UnknownVal();
159}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000160
Ted Kremenek67f28532009-06-17 22:02:04 +0000161SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000162 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000163
164 if (Base.isUnknownOrUndef())
165 return Base;
166
167 Loc BaseL = cast<Loc>(Base);
168 const MemRegion* BaseR = 0;
169
170 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000171 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000172 return UndefinedVal();
173
174 case loc::MemRegionKind:
175 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
176 break;
177
178 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000179 // While these seem funny, this can happen through casts.
180 // FIXME: What we should return is the field offset. For example,
181 // add the field offset to the integer value. That way funny things
182 // like this work properly: &(((struct foo *) 0xa)->f)
183 return Base;
184
185 default:
186 assert ("Unhandled Base.");
187 return Base;
188 }
189
Zhongxing Xud91ee272009-06-23 09:02:15 +0000190 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000191}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000192
Ted Kremenek67f28532009-06-17 22:02:04 +0000193SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekf936f452009-05-04 06:18:28 +0000194 QualType elementType,
195 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000196
Ted Kremenek7d71b292008-12-09 21:20:27 +0000197 if (Base.isUnknownOrUndef())
198 return Base;
199
200 Loc BaseL = cast<Loc>(Base);
Ted Kremenekabb042f2008-12-13 19:24:37 +0000201 const TypedRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000202
203 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000205 // Technically we can get here if people do funny things with casts.
206 return UndefinedVal();
207
Ted Kremenekabb042f2008-12-13 19:24:37 +0000208 case loc::MemRegionKind: {
209 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000210
Ted Kremenek921b0b52009-05-05 00:06:16 +0000211 if (isa<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000212 // int x;
213 // char* y = (char*) &x;
214 // 'y' => ElementRegion(0, VarRegion('x'))
215 // y[0] = 'a';
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000216 return Base;
217 }
218
219
Ted Kremenekabb042f2008-12-13 19:24:37 +0000220 if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) {
221 BaseR = TR;
222 break;
223 }
224
Zhongxing Xua1718c72009-04-03 07:33:13 +0000225 if (const SymbolicRegion* SR = dyn_cast<SymbolicRegion>(R)) {
226 SymbolRef Sym = SR->getSymbol();
227 BaseR = MRMgr.getTypedViewRegion(Sym->getType(getContext()), SR);
228 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000229
Ted Kremenek7d71b292008-12-09 21:20:27 +0000230 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000231 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000232
Ted Kremenek7d71b292008-12-09 21:20:27 +0000233 case loc::ConcreteIntKind:
234 // While these seem funny, this can happen through casts.
235 // FIXME: What we should return is the field offset. For example,
236 // add the field offset to the integer value. That way funny things
237 // like this work properly: &(((struct foo *) 0xa)->f)
238 return Base;
239
240 default:
241 assert ("Unhandled Base.");
242 return Base;
243 }
244
Ted Kremenekabb042f2008-12-13 19:24:37 +0000245 if (BaseR)
Zhongxing Xud91ee272009-06-23 09:02:15 +0000246 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
247 BaseR, getContext()));
Ted Kremenekabb042f2008-12-13 19:24:37 +0000248 else
249 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000250}
251
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000252static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000253 bool foundPointer = false;
254 while (1) {
255 const PointerType *PT = T->getAsPointerType();
256 if (!PT) {
257 if (!foundPointer)
258 return false;
259
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000260 // intptr_t* or intptr_t**, etc?
261 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
262 return true;
263
Ted Kremenek1670e402009-04-11 00:11:10 +0000264 QualType X = C.getCanonicalType(T).getUnqualifiedType();
265 return X == C.VoidTy;
266 }
267
268 foundPointer = true;
269 T = PT->getPointeeType();
270 }
271}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000272
Ted Kremenek67f28532009-06-17 22:02:04 +0000273SVal BasicStoreManager::Retrieve(const GRState *state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000274
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000275 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000276 return UnknownVal();
277
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000278 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000279
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000280 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000281
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000282 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000283 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000284
Ted Kremenek20bd7462009-05-04 07:04:36 +0000285 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000286 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
287 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000288 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000289 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000290
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000291 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek1670e402009-04-11 00:11:10 +0000292 return UnknownVal();
293
Ted Kremenek20bd7462009-05-04 07:04:36 +0000294 // FIXME: Should check for element 0.
295 // Otherwise, strip the element region.
296 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000297 }
298
Ted Kremenekf684d562009-03-05 18:08:28 +0000299 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000300 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000301
Ted Kremenekf684d562009-03-05 18:08:28 +0000302 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000303 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000304 return T ? *T : UnknownVal();
305 }
306
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000307 case loc::ConcreteIntKind:
308 // Some clients may call GetSVal with such an option simply because
309 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000310 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000311 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000312
Ted Kremenek4323a572008-07-10 22:03:41 +0000313 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000314 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000315 break;
316 }
317
318 return UnknownVal();
319}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000320
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000321Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +0000322 if (isa<loc::ConcreteInt>(loc))
323 return store;
324
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000325 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
326 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000327
Zhongxing Xuc2a650a2009-06-28 09:26:15 +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 ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
333 // FIXME: Should check for index 0.
334 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000335
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000336 if (isHigherOrderRawPtr(T, C))
337 R = ER->getSuperRegion();
338 }
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000339
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000340 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
341 return store;
Ted Kremenek9e240492008-10-04 05:50:14 +0000342
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000343 // We only track bindings to self.ivar.
344 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
345 if (IVR->getSuperRegion() != SelfRegion)
Ted Kremenekf59bf482008-07-17 18:38:48 +0000346 return store;
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000347
348 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
349 // Only convert 'V' to a location iff the underlying region type
350 // is a location as well.
351 // FIXME: We are allowing a store of an arbitrary location to
352 // a pointer. We may wish to flag a type error here if the types
353 // are incompatible. This may also cause lots of breakage
354 // elsewhere. Food for thought.
355 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
356 if (TyR->isBoundable() &&
357 Loc::IsLocType(TyR->getValueType(C)))
358 V = X->getLoc();
359 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000360 }
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000361
362 BindingsTy B = GetBindings(store);
363 return V.isUnknown()
364 ? VBFactory.Remove(B, R).getRoot()
365 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenek4323a572008-07-10 22:03:41 +0000366}
367
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000368Store BasicStoreManager::Remove(Store store, Loc loc) {
369 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000370 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000371 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000372
Ted Kremenekf684d562009-03-05 18:08:28 +0000373 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000374 return store;
375
Ted Kremenekf684d562009-03-05 18:08:28 +0000376 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000377 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000378 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000379 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000380 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000381 }
382}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000383
Ted Kremenek9e240492008-10-04 05:50:14 +0000384Store
Ted Kremenek67f28532009-06-17 22:02:04 +0000385BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000386 SymbolReaper& SymReaper,
387 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
388{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000389
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000390 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000391 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000392 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000393
394 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000395 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000396 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
397 if (SymReaper.isLive(Loc, VR->getDecl()))
398 RegionRoots.push_back(VR);
399 else
400 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000401 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000402 else if (isa<ObjCIvarRegion>(I.getKey())) {
403 RegionRoots.push_back(I.getKey());
404 }
405 else
406 continue;
407
408 // Mark the bindings in the data as live.
409 SVal X = I.getData();
410 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
411 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000412 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000413
414 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000415 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000416
Ted Kremenek9e240492008-10-04 05:50:14 +0000417 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000418 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000419 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000420
Ted Kremenek134e7492008-10-17 22:52:40 +0000421 while (MR) {
422 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000423 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000424 break;
425 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000426 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
427 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000428 break;
429
Ted Kremenekf684d562009-03-05 18:08:28 +0000430 Marked.insert(MR);
431 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000432
Ted Kremenek134e7492008-10-17 22:52:40 +0000433 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000434 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
435 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000436
Ted Kremenek134e7492008-10-17 22:52:40 +0000437 if (!isa<loc::MemRegionVal>(X))
438 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000439
Ted Kremenek134e7492008-10-17 22:52:40 +0000440 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
441 RegionRoots.push_back(LVD.getRegion());
442 break;
443 }
444 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
445 MR = R->getSuperRegion();
446 else
447 break;
448 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000449 }
450
451 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000452 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000453 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000454
455 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000456 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000457 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000458
459 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000460 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000461 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000462 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000463
Ted Kremenekf59bf482008-07-17 18:38:48 +0000464 return store;
465}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000466
Ted Kremenekf684d562009-03-05 18:08:28 +0000467Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
468 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
469 CI != CE; ++CI) {
470
471 if (!*CI)
472 continue;
473
474 // Check if the statement is an ivar reference. We only
475 // care about self.ivar.
476 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
477 const Expr *Base = IV->getBase()->IgnoreParenCasts();
478 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
479 if (DR->getDecl() == SelfDecl) {
480 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000481 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000482 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000483 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000484 }
485 }
486 }
487 else
488 St = scanForIvars(*CI, SelfDecl, St);
489 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000490
Ted Kremenekf684d562009-03-05 18:08:28 +0000491 return St;
492}
493
494Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000495 // The LiveVariables information already has a compilation of all VarDecls
496 // used in the function. Iterate through this set, and "symbolicate"
497 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000498 typedef LiveVariables::AnalysisDataTy LVDataTy;
499 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000500 Store St = VBFactory.GetEmptyMap().getRoot();
501
502 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000503 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000504
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000505 // Handle implicit parameters.
506 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
507 const Decl& CD = StateMgr.getCodeDecl();
508 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
509 if (MD->getSelfDecl() == PD) {
510 // Create a region for "self".
511 assert (SelfRegion == 0);
512 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
513 MRMgr.getHeapRegion());
514
Zhongxing Xud91ee272009-06-23 09:02:15 +0000515 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD)),
516 ValMgr.makeLoc(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000517
518 // Scan the method for ivar references. While this requires an
519 // entire AST scan, the cost should not be high in practice.
Douglas Gregor72971342009-04-18 00:02:19 +0000520 St = scanForIvars(MD->getBody(getContext()), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000521 }
522 }
523 }
524 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000525 // Punt on static variables for now.
526 if (VD->getStorageClass() == VarDecl::Static)
527 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000528
529 // Only handle simple types that we can symbolicate.
530 if (!SymbolManager::canSymbolicate(VD->getType()))
531 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000532
Ted Kremenekec099f12009-03-18 22:10:22 +0000533 // Initialize globals and parameters to symbolic values.
534 // Initialize local variables to undefined.
Ted Kremenek25e751a2009-06-23 21:37:46 +0000535 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD);
Ted Kremenekec099f12009-03-18 22:10:22 +0000536 SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) ||
537 isa<ImplicitParamDecl>(VD))
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000538 ? ValMgr.getRegionValueSymbolVal(R)
Ted Kremenekec099f12009-03-18 22:10:22 +0000539 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000540
Zhongxing Xud91ee272009-06-23 09:02:15 +0000541 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000542 }
543 }
544 return St;
545}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000546
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000547Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
548 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000549
Ted Kremeneke53c0692008-08-23 00:50:55 +0000550 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000551
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000552 // BasicStore does not model arrays and structs.
553 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
554 return store;
555
556 if (VD->hasGlobalStorage()) {
557 // Handle variables with global storage: extern, static, PrivateExtern.
558
559 // FIXME:: static variables may have an initializer, but the second time a
560 // function is called those values may not be current. Currently, a function
561 // will not be called more than once.
562
563 // Static global variables should not be visited here.
564 assert(!(VD->getStorageClass() == VarDecl::Static &&
565 VD->isFileVarDecl()));
566
567 // Process static variables.
568 if (VD->getStorageClass() == VarDecl::Static) {
569 // C99: 6.7.8 Initialization
570 // If an object that has static storage duration is not initialized
571 // explicitly, then:
572 // —if it has pointer type, it is initialized to a null pointer;
573 // —if it has arithmetic type, it is initialized to (positive or
574 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000575 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000576 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000577 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000578 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000579 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000580 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000581 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000582 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000583 else {
584 // assert(0 && "ignore other types of variables");
585 }
586 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000587 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000588 }
589 }
590 } else {
591 // Process local scalar variables.
592 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000593 if (Loc::IsLocType(T) || T->isIntegerType()) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000594 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000595 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000596 }
597 }
598
599 return store;
600}
601
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000602void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000603 const char* nl, const char *sep) {
604
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000605 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000606 Out << "Variables:" << nl;
607
608 bool isFirst = true;
609
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000610 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000611 if (isFirst)
612 isFirst = false;
613 else
614 Out << nl;
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000615
Ted Kremenekbe912242009-03-05 16:31:07 +0000616 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000617 I.getData().print(Out);
618 }
619}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000620
Ted Kremenek60dbad82008-09-03 03:06:11 +0000621
622void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000623 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000624
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000625 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000626 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000627
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000628}
629
Ted Kremenek60dbad82008-09-03 03:06:11 +0000630StoreManager::BindingsHandler::~BindingsHandler() {}