blob: 19d641ee9753a4ab1520b0888be46a3bb082d571 [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);
Zhongxing Xu2001b8e2009-06-30 07:41:27 +0000201 const MemRegion* 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
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000219 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
220 BaseR = R;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000221 break;
222 }
223
Ted Kremenek7d71b292008-12-09 21:20:27 +0000224 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000225 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000226
Ted Kremenek7d71b292008-12-09 21:20:27 +0000227 case loc::ConcreteIntKind:
228 // While these seem funny, this can happen through casts.
229 // FIXME: What we should return is the field offset. For example,
230 // add the field offset to the integer value. That way funny things
231 // like this work properly: &(((struct foo *) 0xa)->f)
232 return Base;
233
234 default:
235 assert ("Unhandled Base.");
236 return Base;
237 }
238
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000239 if (BaseR) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000240 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
241 BaseR, getContext()));
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000242 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000243 else
244 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000245}
246
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000247static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000248 bool foundPointer = false;
249 while (1) {
250 const PointerType *PT = T->getAsPointerType();
251 if (!PT) {
252 if (!foundPointer)
253 return false;
254
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000255 // intptr_t* or intptr_t**, etc?
256 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
257 return true;
258
Ted Kremenek1670e402009-04-11 00:11:10 +0000259 QualType X = C.getCanonicalType(T).getUnqualifiedType();
260 return X == C.VoidTy;
261 }
262
263 foundPointer = true;
264 T = PT->getPointeeType();
265 }
266}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000267
Ted Kremenek67f28532009-06-17 22:02:04 +0000268SVal BasicStoreManager::Retrieve(const GRState *state, Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000269
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000270 if (isa<UnknownVal>(loc))
Ted Kremenek4323a572008-07-10 22:03:41 +0000271 return UnknownVal();
272
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000273 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000274
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000275 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000276
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000277 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000278 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000279
Ted Kremenek20bd7462009-05-04 07:04:36 +0000280 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000281 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
282 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000283 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000284 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000285
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000286 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek1670e402009-04-11 00:11:10 +0000287 return UnknownVal();
288
Ted Kremenek20bd7462009-05-04 07:04:36 +0000289 // FIXME: Should check for element 0.
290 // Otherwise, strip the element region.
291 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000292 }
293
Ted Kremenekf684d562009-03-05 18:08:28 +0000294 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000295 return UnknownVal();
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000296
Ted Kremenekf684d562009-03-05 18:08:28 +0000297 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000298 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek4323a572008-07-10 22:03:41 +0000299 return T ? *T : UnknownVal();
300 }
301
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000302 case loc::ConcreteIntKind:
303 // Some clients may call GetSVal with such an option simply because
304 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000305 // invalidate their bindings). Just return Undefined.
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000306 return UndefinedVal();
Ted Kremenek4323a572008-07-10 22:03:41 +0000307
Ted Kremenek4323a572008-07-10 22:03:41 +0000308 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000309 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000310 break;
311 }
312
313 return UnknownVal();
314}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000315
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000316Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +0000317 if (isa<loc::ConcreteInt>(loc))
318 return store;
319
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000320 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
321 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000322
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000323 // Special case: handle store of pointer values (Loc) to pointers via
324 // a cast to intXX_t*, void*, etc. This is needed to handle
325 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
326 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
327 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
328 // FIXME: Should check for index 0.
329 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000330
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000331 if (isHigherOrderRawPtr(T, C))
332 R = ER->getSuperRegion();
333 }
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000334
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000335 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
336 return store;
Ted Kremenek9e240492008-10-04 05:50:14 +0000337
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000338 // We only track bindings to self.ivar.
339 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
340 if (IVR->getSuperRegion() != SelfRegion)
Ted Kremenekf59bf482008-07-17 18:38:48 +0000341 return store;
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000342
343 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
344 // Only convert 'V' to a location iff the underlying region type
345 // is a location as well.
346 // FIXME: We are allowing a store of an arbitrary location to
347 // a pointer. We may wish to flag a type error here if the types
348 // are incompatible. This may also cause lots of breakage
349 // elsewhere. Food for thought.
350 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
351 if (TyR->isBoundable() &&
352 Loc::IsLocType(TyR->getValueType(C)))
353 V = X->getLoc();
354 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000355 }
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000356
357 BindingsTy B = GetBindings(store);
358 return V.isUnknown()
359 ? VBFactory.Remove(B, R).getRoot()
360 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenek4323a572008-07-10 22:03:41 +0000361}
362
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000363Store BasicStoreManager::Remove(Store store, Loc loc) {
364 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000365 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000366 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000367
Ted Kremenekf684d562009-03-05 18:08:28 +0000368 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000369 return store;
370
Ted Kremenekf684d562009-03-05 18:08:28 +0000371 return VBFactory.Remove(GetBindings(store), R).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 ("Remove 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}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000378
Ted Kremenek9e240492008-10-04 05:50:14 +0000379Store
Ted Kremenek67f28532009-06-17 22:02:04 +0000380BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000381 SymbolReaper& SymReaper,
382 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
383{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000384
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000385 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000386 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000387 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000388
389 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000390 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000391 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
392 if (SymReaper.isLive(Loc, VR->getDecl()))
393 RegionRoots.push_back(VR);
394 else
395 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000396 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000397 else if (isa<ObjCIvarRegion>(I.getKey())) {
398 RegionRoots.push_back(I.getKey());
399 }
400 else
401 continue;
402
403 // Mark the bindings in the data as live.
404 SVal X = I.getData();
405 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
406 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000407 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000408
409 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000410 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000411
Ted Kremenek9e240492008-10-04 05:50:14 +0000412 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000413 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000414 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000415
Ted Kremenek134e7492008-10-17 22:52:40 +0000416 while (MR) {
417 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000418 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000419 break;
420 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000421 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
422 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000423 break;
424
Ted Kremenekf684d562009-03-05 18:08:28 +0000425 Marked.insert(MR);
426 SVal X = Retrieve(state, loc::MemRegionVal(MR));
Ted Kremenekf59bf482008-07-17 18:38:48 +0000427
Ted Kremenek134e7492008-10-17 22:52:40 +0000428 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000429 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
430 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000431
Ted Kremenek134e7492008-10-17 22:52:40 +0000432 if (!isa<loc::MemRegionVal>(X))
433 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000434
Ted Kremenek134e7492008-10-17 22:52:40 +0000435 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
436 RegionRoots.push_back(LVD.getRegion());
437 break;
438 }
439 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
440 MR = R->getSuperRegion();
441 else
442 break;
443 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000444 }
445
446 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000447 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000448 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000449
450 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000451 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000452 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000453
454 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000455 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000456 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000457 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000458
Ted Kremenekf59bf482008-07-17 18:38:48 +0000459 return store;
460}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000461
Ted Kremenekf684d562009-03-05 18:08:28 +0000462Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
463 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
464 CI != CE; ++CI) {
465
466 if (!*CI)
467 continue;
468
469 // Check if the statement is an ivar reference. We only
470 // care about self.ivar.
471 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
472 const Expr *Base = IV->getBase()->IgnoreParenCasts();
473 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
474 if (DR->getDecl() == SelfDecl) {
475 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000476 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000477 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000478 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000479 }
480 }
481 }
482 else
483 St = scanForIvars(*CI, SelfDecl, St);
484 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000485
Ted Kremenekf684d562009-03-05 18:08:28 +0000486 return St;
487}
488
489Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000490 // The LiveVariables information already has a compilation of all VarDecls
491 // used in the function. Iterate through this set, and "symbolicate"
492 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000493 typedef LiveVariables::AnalysisDataTy LVDataTy;
494 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000495 Store St = VBFactory.GetEmptyMap().getRoot();
496
497 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000498 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000499
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000500 // Handle implicit parameters.
501 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
502 const Decl& CD = StateMgr.getCodeDecl();
503 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
504 if (MD->getSelfDecl() == PD) {
505 // Create a region for "self".
506 assert (SelfRegion == 0);
507 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
508 MRMgr.getHeapRegion());
509
Zhongxing Xud91ee272009-06-23 09:02:15 +0000510 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD)),
511 ValMgr.makeLoc(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000512
513 // Scan the method for ivar references. While this requires an
514 // entire AST scan, the cost should not be high in practice.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000515 St = scanForIvars(MD->getBody(), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000516 }
517 }
518 }
519 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000520 // Punt on static variables for now.
521 if (VD->getStorageClass() == VarDecl::Static)
522 continue;
Ted Kremenek693de5d2009-03-23 15:42:58 +0000523
524 // Only handle simple types that we can symbolicate.
525 if (!SymbolManager::canSymbolicate(VD->getType()))
526 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000527
Ted Kremenekec099f12009-03-18 22:10:22 +0000528 // Initialize globals and parameters to symbolic values.
529 // Initialize local variables to undefined.
Ted Kremenek25e751a2009-06-23 21:37:46 +0000530 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD);
Ted Kremenek15086362009-07-02 18:25:09 +0000531 SVal X = R->hasGlobalsOrParametersStorage()
532 ? ValMgr.getRegionValueSymbolVal(R)
533 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000534
Zhongxing Xud91ee272009-06-23 09:02:15 +0000535 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000536 }
537 }
538 return St;
539}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000540
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000541Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
542 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000543
Ted Kremeneke53c0692008-08-23 00:50:55 +0000544 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000545
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000546 // BasicStore does not model arrays and structs.
547 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
548 return store;
549
550 if (VD->hasGlobalStorage()) {
551 // Handle variables with global storage: extern, static, PrivateExtern.
552
553 // FIXME:: static variables may have an initializer, but the second time a
554 // function is called those values may not be current. Currently, a function
555 // will not be called more than once.
556
557 // Static global variables should not be visited here.
558 assert(!(VD->getStorageClass() == VarDecl::Static &&
559 VD->isFileVarDecl()));
560
561 // Process static variables.
562 if (VD->getStorageClass() == VarDecl::Static) {
563 // C99: 6.7.8 Initialization
564 // If an object that has static storage duration is not initialized
565 // explicitly, then:
566 // —if it has pointer type, it is initialized to a null pointer;
567 // —if it has arithmetic type, it is initialized to (positive or
568 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000569 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000570 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000571 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000572 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000573 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000574 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000575 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000576 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000577 else {
578 // assert(0 && "ignore other types of variables");
579 }
580 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000581 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000582 }
583 }
584 } else {
585 // Process local scalar variables.
586 QualType T = VD->getType();
Ted Kremenekf6c9f422009-07-03 00:36:16 +0000587 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000588 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000589 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000590 }
591 }
592
593 return store;
594}
595
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000596void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000597 const char* nl, const char *sep) {
598
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000599 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000600 Out << "Variables:" << nl;
601
602 bool isFirst = true;
603
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000604 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000605 if (isFirst)
606 isFirst = false;
607 else
608 Out << nl;
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000609
Ted Kremenekbe912242009-03-05 16:31:07 +0000610 Out << ' ' << I.getKey() << " : ";
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000611 I.getData().print(Out);
612 }
613}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000614
Ted Kremenek60dbad82008-09-03 03:06:11 +0000615
616void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000617 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000618
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000619 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000620 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000621
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000622}
623
Ted Kremenek60dbad82008-09-03 03:06:11 +0000624StoreManager::BindingsHandler::~BindingsHandler() {}