blob: 2b70a964267c660c234fd86b9683d7c22dd9944f [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:
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000041 BasicStoreManager(GRStateManager& mgr)
42 : StoreManager(mgr), VBFactory(mgr.getAllocator()), SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000043
Ted Kremenek9deb0e32008-10-24 20:32:16 +000044 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000045
Ted Kremenek67f28532009-06-17 22:02:04 +000046 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek14453bf2009-03-03 19:02:42 +000047 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000048 }
49
Ted Kremenek32c3fa42009-07-21 21:03:30 +000050 SValuator::CastResult Retrieve(const GRState *state, Loc loc,
Ted Kremenek1004a9f2009-07-29 18:16:25 +000051 QualType T = QualType());
52
53 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
54 const Expr *E, unsigned Count);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000055
Ted Kremenek67f28532009-06-17 22:02:04 +000056 const GRState *Bind(const GRState *state, Loc L, SVal V) {
57 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xu4193eca2008-12-20 06:32:12 +000058 }
59
Ted Kremenekf684d562009-03-05 18:08:28 +000060 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
61
Ted Kremenekc6ed3842009-01-07 22:56:17 +000062 Store BindInternal(Store St, Loc loc, SVal V);
63 Store Remove(Store St, Loc loc);
Ted Kremenek9deb0e32008-10-24 20:32:16 +000064 Store getInitialStore();
Zhongxing Xubc678fd2008-10-07 01:31:04 +000065
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000066 // FIXME: Investigate what is using this. This method should be removed.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000067 virtual Loc getLoc(const VarDecl* VD) {
Zhongxing Xud91ee272009-06-23 09:02:15 +000068 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000069 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000070
Ted Kremenek67f28532009-06-17 22:02:04 +000071 const GRState *BindCompoundLiteral(const GRState *state,
72 const CompoundLiteralExpr* cl,
73 SVal val) {
74 return state;
Ted Kremenek4f090272008-10-27 21:54:31 +000075 }
76
Ted Kremenek67f28532009-06-17 22:02:04 +000077 SVal getLValueVar(const GRState *state, const VarDecl* VD);
78 SVal getLValueString(const GRState *state, const StringLiteral* S);
79 SVal getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +000080 const CompoundLiteralExpr* CL);
Ted Kremenek67f28532009-06-17 22:02:04 +000081 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
82 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl* D);
83 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +000084 SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000085
Ted Kremenek9deb0e32008-10-24 20:32:16 +000086 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
87 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000088 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000089
Ted Kremenek9deb0e32008-10-24 20:32:16 +000090 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
91 /// 'this' object (C++). When used when analyzing a normal function this
92 /// method returns NULL.
Ted Kremenek39fc7152009-03-05 16:41:21 +000093 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000094
Ted Kremenek2ed14be2008-12-05 00:47:52 +000095 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenek67f28532009-06-17 22:02:04 +000096 /// It returns a new Store with these values removed.
97 Store RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +000098 SymbolReaper& SymReaper,
99 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000100
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000101 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000102
Ted Kremenek67f28532009-06-17 22:02:04 +0000103 const GRState *BindDecl(const GRState *state, const VarDecl* VD, SVal InitVal) {
104 return state->makeWithStore(BindDeclInternal(state->getStore(),VD, &InitVal));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000105 }
106
Ted Kremenek67f28532009-06-17 22:02:04 +0000107 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl* VD) {
108 return state->makeWithStore(BindDeclInternal(state->getStore(), VD, 0));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000109 }
110
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000111 Store BindDeclInternal(Store store, const VarDecl* VD, SVal* InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000112
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000113 static inline BindingsTy GetBindings(Store store) {
114 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000115 }
116
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000117 void print(Store store, llvm::raw_ostream& Out, const char* nl,
118 const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000119
120private:
121 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000122};
Ted Kremenek9e240492008-10-04 05:50:14 +0000123
Ted Kremenek4323a572008-07-10 22:03:41 +0000124} // end anonymous namespace
125
126
Ted Kremenek5f81c442008-08-28 23:31:31 +0000127StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
128 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000129}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000130
Ted Kremenek67f28532009-06-17 22:02:04 +0000131SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000132 return ValMgr.makeLoc(MRMgr.getVarRegion(VD));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000133}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000134
Ted Kremenek67f28532009-06-17 22:02:04 +0000135SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000136 const StringLiteral* S) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000137 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000138}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000139
Ted Kremenek67f28532009-06-17 22:02:04 +0000140SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000141 const CompoundLiteralExpr* CL){
Zhongxing Xud91ee272009-06-23 09:02:15 +0000142 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000143}
144
Ted Kremenek67f28532009-06-17 22:02:04 +0000145SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000146 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000147
148 if (Base.isUnknownOrUndef())
149 return Base;
150
151 Loc BaseL = cast<Loc>(Base);
152
153 if (isa<loc::MemRegionVal>(BaseL)) {
154 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
155
156 if (BaseR == SelfRegion)
Zhongxing Xud91ee272009-06-23 09:02:15 +0000157 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekf684d562009-03-05 18:08:28 +0000158 }
159
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000160 return UnknownVal();
161}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000162
Ted Kremenek67f28532009-06-17 22:02:04 +0000163SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000164 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000165
166 if (Base.isUnknownOrUndef())
167 return Base;
168
169 Loc BaseL = cast<Loc>(Base);
170 const MemRegion* BaseR = 0;
171
172 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000173 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000174 return UndefinedVal();
175
176 case loc::MemRegionKind:
177 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
178 break;
179
180 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000181 // While these seem funny, this can happen through casts.
182 // FIXME: What we should return is the field offset. For example,
183 // add the field offset to the integer value. That way funny things
184 // like this work properly: &(((struct foo *) 0xa)->f)
185 return Base;
186
187 default:
188 assert ("Unhandled Base.");
189 return Base;
190 }
191
Zhongxing Xud91ee272009-06-23 09:02:15 +0000192 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000193}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000194
Ted Kremenek67f28532009-06-17 22:02:04 +0000195SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekf936f452009-05-04 06:18:28 +0000196 QualType elementType,
197 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000198
Ted Kremenek7d71b292008-12-09 21:20:27 +0000199 if (Base.isUnknownOrUndef())
200 return Base;
201
202 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu2001b8e2009-06-30 07:41:27 +0000203 const MemRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204
205 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000206 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000207 // Technically we can get here if people do funny things with casts.
208 return UndefinedVal();
209
Ted Kremenekabb042f2008-12-13 19:24:37 +0000210 case loc::MemRegionKind: {
211 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000212
Ted Kremenek921b0b52009-05-05 00:06:16 +0000213 if (isa<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000214 // int x;
215 // char* y = (char*) &x;
216 // 'y' => ElementRegion(0, VarRegion('x'))
217 // y[0] = 'a';
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000218 return Base;
219 }
220
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000221 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
222 BaseR = R;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000223 break;
224 }
225
Ted Kremenek7d71b292008-12-09 21:20:27 +0000226 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000227 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000228
Ted Kremenek7d71b292008-12-09 21:20:27 +0000229 case loc::ConcreteIntKind:
230 // While these seem funny, this can happen through casts.
231 // FIXME: What we should return is the field offset. For example,
232 // add the field offset to the integer value. That way funny things
233 // like this work properly: &(((struct foo *) 0xa)->f)
234 return Base;
235
236 default:
237 assert ("Unhandled Base.");
238 return Base;
239 }
240
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000241 if (BaseR) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000242 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
243 BaseR, getContext()));
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000244 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000245 else
246 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000247}
248
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000249static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000250 bool foundPointer = false;
251 while (1) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000252 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenek1670e402009-04-11 00:11:10 +0000253 if (!PT) {
254 if (!foundPointer)
255 return false;
256
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000257 // intptr_t* or intptr_t**, etc?
258 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
259 return true;
260
Ted Kremenek1670e402009-04-11 00:11:10 +0000261 QualType X = C.getCanonicalType(T).getUnqualifiedType();
262 return X == C.VoidTy;
263 }
264
265 foundPointer = true;
266 T = PT->getPointeeType();
267 }
268}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000269
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000270SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state,
271 Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000272
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000273 if (isa<UnknownVal>(loc))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000274 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000275
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000276 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000277
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000278 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000279
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000280 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000281 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000282
Ted Kremenek20bd7462009-05-04 07:04:36 +0000283 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000284 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
285 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000286 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000287 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000288
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000289 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000290 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek1670e402009-04-11 00:11:10 +0000291
Ted Kremenek20bd7462009-05-04 07:04:36 +0000292 // FIXME: Should check for element 0.
293 // Otherwise, strip the element region.
294 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000295 }
296
Ted Kremenekf684d562009-03-05 18:08:28 +0000297 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000298 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000299
Ted Kremenekf684d562009-03-05 18:08:28 +0000300 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000301 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000302 return SValuator::CastResult(state, T ? *T : UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000303 }
304
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000305 case loc::ConcreteIntKind:
306 // Some clients may call GetSVal with such an option simply because
307 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000308 // invalidate their bindings). Just return Undefined.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000309 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000310
Ted Kremenek4323a572008-07-10 22:03:41 +0000311 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000312 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000313 break;
314 }
315
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000316 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000317}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000318
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000319Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +0000320 if (isa<loc::ConcreteInt>(loc))
321 return store;
322
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000323 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
324 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000325
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000326 // Special case: handle store of pointer values (Loc) to pointers via
327 // a cast to intXX_t*, void*, etc. This is needed to handle
328 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
329 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
330 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
331 // FIXME: Should check for index 0.
332 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000333
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000334 if (isHigherOrderRawPtr(T, C))
335 R = ER->getSuperRegion();
336 }
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000337
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000338 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
339 return store;
Ted Kremenek9e240492008-10-04 05:50:14 +0000340
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000341 // We only track bindings to self.ivar.
342 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
343 if (IVR->getSuperRegion() != SelfRegion)
Ted Kremenekf59bf482008-07-17 18:38:48 +0000344 return store;
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000345
346 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
347 // Only convert 'V' to a location iff the underlying region type
348 // is a location as well.
349 // FIXME: We are allowing a store of an arbitrary location to
350 // a pointer. We may wish to flag a type error here if the types
351 // are incompatible. This may also cause lots of breakage
352 // elsewhere. Food for thought.
353 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
354 if (TyR->isBoundable() &&
355 Loc::IsLocType(TyR->getValueType(C)))
356 V = X->getLoc();
357 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000358 }
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000359
360 BindingsTy B = GetBindings(store);
361 return V.isUnknown()
362 ? VBFactory.Remove(B, R).getRoot()
363 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenek4323a572008-07-10 22:03:41 +0000364}
365
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000366Store BasicStoreManager::Remove(Store store, Loc loc) {
367 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000368 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000369 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000370
Ted Kremenekf684d562009-03-05 18:08:28 +0000371 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000372 return store;
373
Ted Kremenekf684d562009-03-05 18:08:28 +0000374 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000375 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000376 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000377 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000378 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000379 }
380}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000381
Ted Kremenek9e240492008-10-04 05:50:14 +0000382Store
Ted Kremenek67f28532009-06-17 22:02:04 +0000383BasicStoreManager::RemoveDeadBindings(const GRState *state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000384 SymbolReaper& SymReaper,
385 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
386{
Ted Kremenekf59bf482008-07-17 18:38:48 +0000387
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000388 Store store = state->getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000389 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000390 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000391
392 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000393 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000394 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
395 if (SymReaper.isLive(Loc, VR->getDecl()))
396 RegionRoots.push_back(VR);
397 else
398 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000399 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000400 else if (isa<ObjCIvarRegion>(I.getKey())) {
401 RegionRoots.push_back(I.getKey());
402 }
403 else
404 continue;
405
406 // Mark the bindings in the data as live.
407 SVal X = I.getData();
408 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
409 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000410 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000411
412 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000413 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000414
Ted Kremenek9e240492008-10-04 05:50:14 +0000415 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000416 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000417 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000418
Ted Kremenek134e7492008-10-17 22:52:40 +0000419 while (MR) {
420 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000421 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000422 break;
423 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000424 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
425 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000426 break;
427
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000428 Marked.insert(MR);
429 SVal X = Retrieve(state, loc::MemRegionVal(MR)).getSVal();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000430
Ted Kremenek134e7492008-10-17 22:52:40 +0000431 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000432 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
433 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000434
Ted Kremenek134e7492008-10-17 22:52:40 +0000435 if (!isa<loc::MemRegionVal>(X))
436 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000437
Ted Kremenek134e7492008-10-17 22:52:40 +0000438 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
439 RegionRoots.push_back(LVD.getRegion());
440 break;
441 }
442 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
443 MR = R->getSuperRegion();
444 else
445 break;
446 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000447 }
448
449 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000450 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000451 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000452
453 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000454 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000455 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000456
457 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000458 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000459 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000460 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000461
Ted Kremenekf59bf482008-07-17 18:38:48 +0000462 return store;
463}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000464
Ted Kremenekf684d562009-03-05 18:08:28 +0000465Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
466 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
467 CI != CE; ++CI) {
468
469 if (!*CI)
470 continue;
471
472 // Check if the statement is an ivar reference. We only
473 // care about self.ivar.
474 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
475 const Expr *Base = IV->getBase()->IgnoreParenCasts();
476 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
477 if (DR->getDecl() == SelfDecl) {
478 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000479 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000480 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000481 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000482 }
483 }
484 }
485 else
486 St = scanForIvars(*CI, SelfDecl, St);
487 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000488
Ted Kremenekf684d562009-03-05 18:08:28 +0000489 return St;
490}
491
492Store BasicStoreManager::getInitialStore() {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000493 // The LiveVariables information already has a compilation of all VarDecls
494 // used in the function. Iterate through this set, and "symbolicate"
495 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000496 typedef LiveVariables::AnalysisDataTy LVDataTy;
497 LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000498 Store St = VBFactory.GetEmptyMap().getRoot();
499
500 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000501 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000502
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000503 // Handle implicit parameters.
504 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
505 const Decl& CD = StateMgr.getCodeDecl();
506 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
507 if (MD->getSelfDecl() == PD) {
508 // Create a region for "self".
509 assert (SelfRegion == 0);
510 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
511 MRMgr.getHeapRegion());
512
Zhongxing Xud91ee272009-06-23 09:02:15 +0000513 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD)),
514 ValMgr.makeLoc(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000515
516 // Scan the method for ivar references. While this requires an
517 // entire AST scan, the cost should not be high in practice.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000518 St = scanForIvars(MD->getBody(), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000519 }
520 }
521 }
522 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenek693de5d2009-03-23 15:42:58 +0000523 // Only handle simple types that we can symbolicate.
524 if (!SymbolManager::canSymbolicate(VD->getType()))
525 continue;
Ted Kremenekcaa37242008-08-19 16:51:45 +0000526
Ted Kremenekec099f12009-03-18 22:10:22 +0000527 // Initialize globals and parameters to symbolic values.
528 // Initialize local variables to undefined.
Ted Kremenek25e751a2009-06-23 21:37:46 +0000529 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD);
Ted Kremenek15086362009-07-02 18:25:09 +0000530 SVal X = R->hasGlobalsOrParametersStorage()
531 ? ValMgr.getRegionValueSymbolVal(R)
532 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000533
Zhongxing Xud91ee272009-06-23 09:02:15 +0000534 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000535 }
536 }
537 return St;
538}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000539
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000540Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
541 SVal* InitVal) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000542
Ted Kremeneke53c0692008-08-23 00:50:55 +0000543 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremenek42577d12008-11-12 19:18:35 +0000544
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000545 // BasicStore does not model arrays and structs.
546 if (VD->getType()->isArrayType() || VD->getType()->isStructureType())
547 return store;
548
549 if (VD->hasGlobalStorage()) {
550 // Handle variables with global storage: extern, static, PrivateExtern.
551
552 // FIXME:: static variables may have an initializer, but the second time a
553 // function is called those values may not be current. Currently, a function
554 // will not be called more than once.
555
556 // Static global variables should not be visited here.
557 assert(!(VD->getStorageClass() == VarDecl::Static &&
558 VD->isFileVarDecl()));
559
560 // Process static variables.
561 if (VD->getStorageClass() == VarDecl::Static) {
562 // C99: 6.7.8 Initialization
563 // If an object that has static storage duration is not initialized
564 // explicitly, then:
565 // —if it has pointer type, it is initialized to a null pointer;
566 // —if it has arithmetic type, it is initialized to (positive or
567 // unsigned) zero;
Ted Kremenek42577d12008-11-12 19:18:35 +0000568 if (!InitVal) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000569 QualType T = VD->getType();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000570 if (Loc::IsLocType(T))
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000571 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000572 loc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000573 else if (T->isIntegerType())
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000574 store = BindInternal(store, getLoc(VD),
Zhongxing Xu8485ec62008-10-21 06:27:32 +0000575 nonloc::ConcreteInt(BasicVals.getValue(0, T)));
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000576 else {
577 // assert(0 && "ignore other types of variables");
578 }
579 } else {
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000580 store = BindInternal(store, getLoc(VD), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000581 }
582 }
583 } else {
584 // Process local scalar variables.
585 QualType T = VD->getType();
Ted Kremenekf6c9f422009-07-03 00:36:16 +0000586 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000587 SVal V = InitVal ? *InitVal : UndefinedVal();
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000588 store = BindInternal(store, getLoc(VD), V);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000589 }
590 }
591
592 return store;
593}
594
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000595void BasicStoreManager::print(Store store, llvm::raw_ostream& Out,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000596 const char* nl, const char *sep) {
597
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000598 BindingsTy B = GetBindings(store);
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000599 Out << "Variables:" << nl;
600
601 bool isFirst = true;
602
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000603 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I) {
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000604 if (isFirst)
605 isFirst = false;
606 else
607 Out << nl;
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000608
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000609 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000610 }
611}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000612
Ted Kremenek60dbad82008-09-03 03:06:11 +0000613
614void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000615 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000616
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000617 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000618 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000619
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000620}
621
Ted Kremenek60dbad82008-09-03 03:06:11 +0000622StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000623
624//===----------------------------------------------------------------------===//
625// Binding invalidation.
626//===----------------------------------------------------------------------===//
627
628const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
629 const MemRegion *R,
630 const Expr *E,
631 unsigned Count) {
632 R = R->getBaseRegion();
633
634 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
635 return state;
636
637 // We only track bindings to self.ivar.
638 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
639 if (IVR->getSuperRegion() != SelfRegion)
640 return state;
641
642 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
643 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
644 return Bind(state, loc::MemRegionVal(R), V);
645}
646