blob: 65efb6614839c63f7df64f92f84f5678655192bc [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"
Zhongxing Xu17fd8632009-08-17 06:19:58 +000016#include "clang/Analysis/PathSensitive/AnalysisContext.h"
Ted Kremenekcaa37242008-08-19 16:51:45 +000017#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000018#include "llvm/ADT/ImmutableMap.h"
19#include "llvm/Support/Compiler.h"
Ted Kremeneka622d8c2008-08-19 22:24:03 +000020#include "llvm/Support/Streams.h"
Ted Kremenek4323a572008-07-10 22:03:41 +000021
22using namespace clang;
23
Ted Kremenek2c8e7882009-03-05 16:32:59 +000024typedef llvm::ImmutableMap<const MemRegion*,SVal> BindingsTy;
Ted Kremenek60dbad82008-09-03 03:06:11 +000025
Ted Kremenek4323a572008-07-10 22:03:41 +000026namespace {
27
Ted Kremenek59e8f112009-03-03 01:35:36 +000028class VISIBILITY_HIDDEN BasicStoreSubRegionMap : public SubRegionMap {
29public:
30 BasicStoreSubRegionMap() {}
31
Ted Kremenek5dc27462009-03-03 02:51:43 +000032 bool iterSubRegions(const MemRegion* R, Visitor& V) const {
Ted Kremenek39fc7152009-03-05 16:41:21 +000033 return true; // Do nothing. No subregions.
Ted Kremenek59e8f112009-03-03 01:35:36 +000034 }
35};
36
Ted Kremenek4323a572008-07-10 22:03:41 +000037class VISIBILITY_HIDDEN BasicStoreManager : public StoreManager {
Ted Kremenek2c8e7882009-03-05 16:32:59 +000038 BindingsTy::Factory VBFactory;
Ted Kremenek9deb0e32008-10-24 20:32:16 +000039 const MemRegion* SelfRegion;
Ted Kremenek4323a572008-07-10 22:03:41 +000040
41public:
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000042 BasicStoreManager(GRStateManager& mgr)
43 : StoreManager(mgr), VBFactory(mgr.getAllocator()), SelfRegion(0) {}
Ted Kremenekd0c4b282008-08-25 19:33:03 +000044
Ted Kremenek9deb0e32008-10-24 20:32:16 +000045 ~BasicStoreManager() {}
Ted Kremenek4323a572008-07-10 22:03:41 +000046
Ted Kremenek67f28532009-06-17 22:02:04 +000047 SubRegionMap *getSubRegionMap(const GRState *state) {
Ted Kremenek14453bf2009-03-03 19:02:42 +000048 return new BasicStoreSubRegionMap();
Ted Kremenek59e8f112009-03-03 01:35:36 +000049 }
50
Ted Kremenek32c3fa42009-07-21 21:03:30 +000051 SValuator::CastResult Retrieve(const GRState *state, Loc loc,
Ted Kremenek1004a9f2009-07-29 18:16:25 +000052 QualType T = QualType());
53
54 const GRState *InvalidateRegion(const GRState *state, const MemRegion *R,
55 const Expr *E, unsigned Count);
Zhongxing Xu4193eca2008-12-20 06:32:12 +000056
Ted Kremenek67f28532009-06-17 22:02:04 +000057 const GRState *Bind(const GRState *state, Loc L, SVal V) {
58 return state->makeWithStore(BindInternal(state->getStore(), L, V));
Zhongxing Xu4193eca2008-12-20 06:32:12 +000059 }
60
Ted Kremenekf684d562009-03-05 18:08:28 +000061 Store scanForIvars(Stmt *B, const Decl* SelfDecl, Store St);
62
Ted Kremenekc6ed3842009-01-07 22:56:17 +000063 Store BindInternal(Store St, Loc loc, SVal V);
64 Store Remove(Store St, Loc loc);
Zhongxing Xu17fd8632009-08-17 06:19:58 +000065 Store getInitialStore(const LocationContext *InitLoc);
Zhongxing Xubc678fd2008-10-07 01:31:04 +000066
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000067 // FIXME: Investigate what is using this. This method should be removed.
Ted Kremenekd17da2b2009-08-21 22:28:32 +000068 virtual Loc getLoc(const VarDecl* VD, const LocationContext *LC) {
69 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Zhongxing Xubc678fd2008-10-07 01:31:04 +000070 }
Ted Kremenekd9bc33e2008-10-17 00:51:01 +000071
Ted Kremenek67f28532009-06-17 22:02:04 +000072 const GRState *BindCompoundLiteral(const GRState *state,
73 const CompoundLiteralExpr* cl,
74 SVal val) {
75 return state;
Ted Kremenek4f090272008-10-27 21:54:31 +000076 }
77
Ted Kremenekd17da2b2009-08-21 22:28:32 +000078 SVal getLValueVar(const GRState *state, const VarDecl *VD,
79 const LocationContext *LC);
80 SVal getLValueString(const GRState *state, const StringLiteral *S);
Ted Kremenek67f28532009-06-17 22:02:04 +000081 SVal getLValueCompoundLiteral(const GRState *state,
Ted Kremenekd17da2b2009-08-21 22:28:32 +000082 const CompoundLiteralExpr *CL);
Ted Kremenek67f28532009-06-17 22:02:04 +000083 SVal getLValueIvar(const GRState *state, const ObjCIvarDecl* D, SVal Base);
Ted Kremenekd17da2b2009-08-21 22:28:32 +000084 SVal getLValueField(const GRState *state, SVal Base, const FieldDecl *D);
Ted Kremenek67f28532009-06-17 22:02:04 +000085 SVal getLValueElement(const GRState *state, QualType elementType,
Ted Kremenekf936f452009-05-04 06:18:28 +000086 SVal Base, SVal Offset);
Zhongxing Xue1911af2008-10-23 03:10:39 +000087
Ted Kremenek9deb0e32008-10-24 20:32:16 +000088 /// ArrayToPointer - Used by GRExprEngine::VistCast to handle implicit
89 /// conversions between arrays and pointers.
Zhongxing Xuf1d537f2009-03-30 05:55:46 +000090 SVal ArrayToPointer(Loc Array) { return Array; }
Zhongxing Xudc0a25d2008-11-16 04:07:26 +000091
Ted Kremenek9deb0e32008-10-24 20:32:16 +000092 /// getSelfRegion - Returns the region for the 'self' (Objective-C) or
93 /// 'this' object (C++). When used when analyzing a normal function this
94 /// method returns NULL.
Ted Kremenek39fc7152009-03-05 16:41:21 +000095 const MemRegion* getSelfRegion(Store) { return SelfRegion; }
Ted Kremenek9deb0e32008-10-24 20:32:16 +000096
Ted Kremenek2ed14be2008-12-05 00:47:52 +000097 /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
Ted Kremenek2f26bc32009-08-02 04:45:08 +000098 /// It updatees the GRState object in place with the values removed.
99 void RemoveDeadBindings(GRState &state, Stmt* Loc, SymbolReaper& SymReaper,
100 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000101
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000102 void iterBindings(Store store, BindingsHandler& f);
Ted Kremenek60dbad82008-09-03 03:06:11 +0000103
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000104 const GRState *BindDecl(const GRState *state, const VarDecl *VD,
105 const LocationContext *LC, SVal InitVal) {
106 return state->makeWithStore(BindDeclInternal(state->getStore(),VD, LC,
107 &InitVal));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000108 }
109
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000110 const GRState *BindDeclWithNoInit(const GRState *state, const VarDecl *VD,
111 const LocationContext *LC) {
112 return state->makeWithStore(BindDeclInternal(state->getStore(), VD, LC, 0));
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000113 }
114
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000115 Store BindDeclInternal(Store store, const VarDecl *VD,
116 const LocationContext *LC, SVal *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000117
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000118 static inline BindingsTy GetBindings(Store store) {
119 return BindingsTy(static_cast<const BindingsTy::TreeTy*>(store));
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000120 }
121
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000122 void print(Store store, llvm::raw_ostream& Out, const char* nl,
123 const char *sep);
Zhongxing Xua1718c72009-04-03 07:33:13 +0000124
125private:
126 ASTContext& getContext() { return StateMgr.getContext(); }
Ted Kremenek60dbad82008-09-03 03:06:11 +0000127};
Ted Kremenek9e240492008-10-04 05:50:14 +0000128
Ted Kremenek4323a572008-07-10 22:03:41 +0000129} // end anonymous namespace
130
131
Ted Kremenek5f81c442008-08-28 23:31:31 +0000132StoreManager* clang::CreateBasicStoreManager(GRStateManager& StMgr) {
133 return new BasicStoreManager(StMgr);
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000134}
Zhongxing Xu933c3e12008-10-21 06:54:23 +0000135
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000136SVal BasicStoreManager::getLValueVar(const GRState *state, const VarDecl* VD,
137 const LocationContext *LC) {
138 return ValMgr.makeLoc(MRMgr.getVarRegion(VD, LC));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000139}
Zhongxing Xu143bf822008-10-25 14:18:57 +0000140
Ted Kremenek67f28532009-06-17 22:02:04 +0000141SVal BasicStoreManager::getLValueString(const GRState *state,
Zhongxing Xu143bf822008-10-25 14:18:57 +0000142 const StringLiteral* S) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000143 return ValMgr.makeLoc(MRMgr.getStringRegion(S));
Zhongxing Xu143bf822008-10-25 14:18:57 +0000144}
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000145
Ted Kremenek67f28532009-06-17 22:02:04 +0000146SVal BasicStoreManager::getLValueCompoundLiteral(const GRState *state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000147 const CompoundLiteralExpr* CL){
Zhongxing Xud91ee272009-06-23 09:02:15 +0000148 return ValMgr.makeLoc(MRMgr.getCompoundLiteralRegion(CL));
Zhongxing Xuf22679e2008-11-07 10:38:33 +0000149}
150
Ted Kremenek67f28532009-06-17 22:02:04 +0000151SVal BasicStoreManager::getLValueIvar(const GRState *state, const ObjCIvarDecl* D,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000152 SVal Base) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000153
154 if (Base.isUnknownOrUndef())
155 return Base;
156
157 Loc BaseL = cast<Loc>(Base);
158
159 if (isa<loc::MemRegionVal>(BaseL)) {
160 const MemRegion *BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
161
162 if (BaseR == SelfRegion)
Zhongxing Xud91ee272009-06-23 09:02:15 +0000163 return ValMgr.makeLoc(MRMgr.getObjCIvarRegion(D, BaseR));
Ted Kremenekf684d562009-03-05 18:08:28 +0000164 }
165
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000166 return UnknownVal();
167}
Ted Kremenek6eddeb12008-12-13 21:49:13 +0000168
Ted Kremenek67f28532009-06-17 22:02:04 +0000169SVal BasicStoreManager::getLValueField(const GRState *state, SVal Base,
Zhongxing Xuc92e5fe2008-10-22 09:00:19 +0000170 const FieldDecl* D) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000171
172 if (Base.isUnknownOrUndef())
173 return Base;
174
175 Loc BaseL = cast<Loc>(Base);
176 const MemRegion* BaseR = 0;
177
178 switch(BaseL.getSubKind()) {
Ted Kremenek993f1c72008-10-17 20:28:54 +0000179 case loc::GotoLabelKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000180 return UndefinedVal();
181
182 case loc::MemRegionKind:
183 BaseR = cast<loc::MemRegionVal>(BaseL).getRegion();
184 break;
185
186 case loc::ConcreteIntKind:
Ted Kremenek993f1c72008-10-17 20:28:54 +0000187 // While these seem funny, this can happen through casts.
188 // FIXME: What we should return is the field offset. For example,
189 // add the field offset to the integer value. That way funny things
190 // like this work properly: &(((struct foo *) 0xa)->f)
191 return Base;
192
193 default:
194 assert ("Unhandled Base.");
195 return Base;
196 }
197
Zhongxing Xud91ee272009-06-23 09:02:15 +0000198 return ValMgr.makeLoc(MRMgr.getFieldRegion(D, BaseR));
Ted Kremenekd9bc33e2008-10-17 00:51:01 +0000199}
Ted Kremenekd0c4b282008-08-25 19:33:03 +0000200
Ted Kremenek67f28532009-06-17 22:02:04 +0000201SVal BasicStoreManager::getLValueElement(const GRState *state,
Ted Kremenekf936f452009-05-04 06:18:28 +0000202 QualType elementType,
203 SVal Base, SVal Offset) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000204
Ted Kremenek7d71b292008-12-09 21:20:27 +0000205 if (Base.isUnknownOrUndef())
206 return Base;
207
208 Loc BaseL = cast<Loc>(Base);
Zhongxing Xu2001b8e2009-06-30 07:41:27 +0000209 const MemRegion* BaseR = 0;
Ted Kremenek7d71b292008-12-09 21:20:27 +0000210
211 switch(BaseL.getSubKind()) {
Ted Kremenek7d71b292008-12-09 21:20:27 +0000212 case loc::GotoLabelKind:
Ted Kremenek7d71b292008-12-09 21:20:27 +0000213 // Technically we can get here if people do funny things with casts.
214 return UndefinedVal();
215
Ted Kremenekabb042f2008-12-13 19:24:37 +0000216 case loc::MemRegionKind: {
217 const MemRegion *R = cast<loc::MemRegionVal>(BaseL).getRegion();
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000218
Ted Kremenek921b0b52009-05-05 00:06:16 +0000219 if (isa<ElementRegion>(R)) {
Zhongxing Xub5b848e2009-05-04 08:52:47 +0000220 // int x;
221 // char* y = (char*) &x;
222 // 'y' => ElementRegion(0, VarRegion('x'))
223 // y[0] = 'a';
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000224 return Base;
225 }
226
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000227 if (isa<TypedRegion>(R) || isa<SymbolicRegion>(R)) {
228 BaseR = R;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000229 break;
230 }
231
Ted Kremenek7d71b292008-12-09 21:20:27 +0000232 break;
Ted Kremenekabb042f2008-12-13 19:24:37 +0000233 }
Ted Kremenekd76d47e2009-01-27 18:29:03 +0000234
Ted Kremenek7d71b292008-12-09 21:20:27 +0000235 case loc::ConcreteIntKind:
236 // While these seem funny, this can happen through casts.
237 // FIXME: What we should return is the field offset. For example,
238 // add the field offset to the integer value. That way funny things
239 // like this work properly: &(((struct foo *) 0xa)->f)
240 return Base;
241
242 default:
243 assert ("Unhandled Base.");
244 return Base;
245 }
246
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000247 if (BaseR) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000248 return ValMgr.makeLoc(MRMgr.getElementRegion(elementType, UnknownVal(),
249 BaseR, getContext()));
Ted Kremenek6bf01d62009-06-30 20:24:11 +0000250 }
Ted Kremenekabb042f2008-12-13 19:24:37 +0000251 else
252 return UnknownVal();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000253}
254
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000255static bool isHigherOrderRawPtr(QualType T, ASTContext &C) {
Ted Kremenek1670e402009-04-11 00:11:10 +0000256 bool foundPointer = false;
257 while (1) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000258 const PointerType *PT = T->getAs<PointerType>();
Ted Kremenek1670e402009-04-11 00:11:10 +0000259 if (!PT) {
260 if (!foundPointer)
261 return false;
262
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000263 // intptr_t* or intptr_t**, etc?
264 if (T->isIntegerType() && C.getTypeSize(T) == C.getTypeSize(C.VoidPtrTy))
265 return true;
266
Ted Kremenek1670e402009-04-11 00:11:10 +0000267 QualType X = C.getCanonicalType(T).getUnqualifiedType();
268 return X == C.VoidTy;
269 }
270
271 foundPointer = true;
272 T = PT->getPointeeType();
273 }
274}
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000275
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000276SValuator::CastResult BasicStoreManager::Retrieve(const GRState *state,
277 Loc loc, QualType T) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000278
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000279 if (isa<UnknownVal>(loc))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000280 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000281
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000282 assert (!isa<UndefinedVal>(loc));
Ted Kremenek4323a572008-07-10 22:03:41 +0000283
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000284 switch (loc.getSubKind()) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000285
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000286 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000287 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000288
Ted Kremenek20bd7462009-05-04 07:04:36 +0000289 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000290 // Just support void**, void***, intptr_t*, intptr_t**, etc., for now.
291 // This is needed to handle OSCompareAndSwapPtr() and friends.
Ted Kremenek1670e402009-04-11 00:11:10 +0000292 ASTContext &Ctx = StateMgr.getContext();
Zhongxing Xuff697822009-05-09 00:50:33 +0000293 QualType T = ER->getLocationType(Ctx);
Ted Kremenek1670e402009-04-11 00:11:10 +0000294
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000295 if (!isHigherOrderRawPtr(T, Ctx))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000296 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek1670e402009-04-11 00:11:10 +0000297
Ted Kremenek20bd7462009-05-04 07:04:36 +0000298 // FIXME: Should check for element 0.
299 // Otherwise, strip the element region.
300 R = ER->getSuperRegion();
Ted Kremenek1670e402009-04-11 00:11:10 +0000301 }
302
Ted Kremenekf684d562009-03-05 18:08:28 +0000303 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000304 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek2ed14be2008-12-05 00:47:52 +0000305
Ted Kremenekf684d562009-03-05 18:08:28 +0000306 BindingsTy B = GetBindings(state->getStore());
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000307 BindingsTy::data_type* T = B.lookup(R);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000308 return SValuator::CastResult(state, T ? *T : UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000309 }
310
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000311 case loc::ConcreteIntKind:
312 // Some clients may call GetSVal with such an option simply because
313 // they are doing a quick scan through their Locs (potentially to
Ted Kremenek4323a572008-07-10 22:03:41 +0000314 // invalidate their bindings). Just return Undefined.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000315 return SValuator::CastResult(state, UndefinedVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000316
Ted Kremenek4323a572008-07-10 22:03:41 +0000317 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000318 assert (false && "Invalid Loc.");
Ted Kremenek4323a572008-07-10 22:03:41 +0000319 break;
320 }
321
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000322 return SValuator::CastResult(state, UnknownVal());
Ted Kremenek4323a572008-07-10 22:03:41 +0000323}
Ted Kremenek97ed4f62008-10-17 00:03:18 +0000324
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000325Store BasicStoreManager::BindInternal(Store store, Loc loc, SVal V) {
Zhongxing Xu87453d12009-06-28 10:16:11 +0000326 if (isa<loc::ConcreteInt>(loc))
327 return store;
328
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000329 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
330 ASTContext &C = StateMgr.getContext();
Ted Kremenek9e240492008-10-04 05:50:14 +0000331
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000332 // Special case: handle store of pointer values (Loc) to pointers via
333 // a cast to intXX_t*, void*, etc. This is needed to handle
334 // OSCompareAndSwap32Barrier/OSCompareAndSwap64Barrier.
335 if (isa<Loc>(V) || isa<nonloc::LocAsInteger>(V))
336 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
337 // FIXME: Should check for index 0.
338 QualType T = ER->getLocationType(C);
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000339
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000340 if (isHigherOrderRawPtr(T, C))
341 R = ER->getSuperRegion();
342 }
Ted Kremenek5fa93d52009-04-29 16:03:27 +0000343
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000344 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
345 return store;
Ted Kremenek9e240492008-10-04 05:50:14 +0000346
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000347 // We only track bindings to self.ivar.
348 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
349 if (IVR->getSuperRegion() != SelfRegion)
Ted Kremenekf59bf482008-07-17 18:38:48 +0000350 return store;
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000351
352 if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&V)) {
353 // Only convert 'V' to a location iff the underlying region type
354 // is a location as well.
355 // FIXME: We are allowing a store of an arbitrary location to
356 // a pointer. We may wish to flag a type error here if the types
357 // are incompatible. This may also cause lots of breakage
358 // elsewhere. Food for thought.
359 if (const TypedRegion *TyR = dyn_cast<TypedRegion>(R)) {
360 if (TyR->isBoundable() &&
361 Loc::IsLocType(TyR->getValueType(C)))
362 V = X->getLoc();
363 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000364 }
Zhongxing Xuc2a650a2009-06-28 09:26:15 +0000365
366 BindingsTy B = GetBindings(store);
367 return V.isUnknown()
368 ? VBFactory.Remove(B, R).getRoot()
369 : VBFactory.Add(B, R, V).getRoot();
Ted Kremenek4323a572008-07-10 22:03:41 +0000370}
371
Ted Kremenekc6ed3842009-01-07 22:56:17 +0000372Store BasicStoreManager::Remove(Store store, Loc loc) {
373 switch (loc.getSubKind()) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000374 case loc::MemRegionKind: {
Ted Kremenekf684d562009-03-05 18:08:28 +0000375 const MemRegion* R = cast<loc::MemRegionVal>(loc).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +0000376
Ted Kremenekf684d562009-03-05 18:08:28 +0000377 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
Ted Kremenek9e240492008-10-04 05:50:14 +0000378 return store;
379
Ted Kremenekf684d562009-03-05 18:08:28 +0000380 return VBFactory.Remove(GetBindings(store), R).getRoot();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000381 }
Ted Kremenek4323a572008-07-10 22:03:41 +0000382 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000383 assert ("Remove for given Loc type not yet implemented.");
Ted Kremenekf59bf482008-07-17 18:38:48 +0000384 return store;
Ted Kremenek4323a572008-07-10 22:03:41 +0000385 }
386}
Ted Kremenekf59bf482008-07-17 18:38:48 +0000387
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000388void
389BasicStoreManager::RemoveDeadBindings(GRState &state, Stmt* Loc,
Ted Kremenek241677a2009-01-21 22:26:05 +0000390 SymbolReaper& SymReaper,
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000391 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
392{
393 Store store = state.getStore();
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000394 BindingsTy B = GetBindings(store);
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000395 typedef SVal::symbol_iterator symbol_iterator;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000396
397 // Iterate over the variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000398 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000399 if (const VarRegion *VR = dyn_cast<VarRegion>(I.getKey())) {
400 if (SymReaper.isLive(Loc, VR->getDecl()))
401 RegionRoots.push_back(VR);
402 else
403 continue;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000404 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000405 else if (isa<ObjCIvarRegion>(I.getKey())) {
406 RegionRoots.push_back(I.getKey());
407 }
408 else
409 continue;
410
411 // Mark the bindings in the data as live.
412 SVal X = I.getData();
413 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
414 SymReaper.markLive(*SI);
Ted Kremenekbe912242009-03-05 16:31:07 +0000415 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000416
417 // Scan for live variables and live symbols.
Ted Kremenekf684d562009-03-05 18:08:28 +0000418 llvm::SmallPtrSet<const MemRegion*, 10> Marked;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000419
Ted Kremenek9e240492008-10-04 05:50:14 +0000420 while (!RegionRoots.empty()) {
Ted Kremenek134e7492008-10-17 22:52:40 +0000421 const MemRegion* MR = RegionRoots.back();
Ted Kremenek9e240492008-10-04 05:50:14 +0000422 RegionRoots.pop_back();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000423
Ted Kremenek134e7492008-10-17 22:52:40 +0000424 while (MR) {
425 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(MR)) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000426 SymReaper.markLive(SymR->getSymbol());
Ted Kremenek134e7492008-10-17 22:52:40 +0000427 break;
428 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000429 else if (isa<VarRegion>(MR) || isa<ObjCIvarRegion>(MR)) {
430 if (Marked.count(MR))
Ted Kremenek134e7492008-10-17 22:52:40 +0000431 break;
432
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000433 Marked.insert(MR);
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000434 SVal X = Retrieve(&state, loc::MemRegionVal(MR)).getSVal();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000435
Ted Kremenek134e7492008-10-17 22:52:40 +0000436 // FIXME: We need to handle symbols nested in region definitions.
Ted Kremenek241677a2009-01-21 22:26:05 +0000437 for (symbol_iterator SI=X.symbol_begin(),SE=X.symbol_end();SI!=SE;++SI)
438 SymReaper.markLive(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000439
Ted Kremenek134e7492008-10-17 22:52:40 +0000440 if (!isa<loc::MemRegionVal>(X))
441 break;
Ted Kremenekf59bf482008-07-17 18:38:48 +0000442
Ted Kremenek134e7492008-10-17 22:52:40 +0000443 const loc::MemRegionVal& LVD = cast<loc::MemRegionVal>(X);
444 RegionRoots.push_back(LVD.getRegion());
445 break;
446 }
447 else if (const SubRegion* R = dyn_cast<SubRegion>(MR))
448 MR = R->getSuperRegion();
449 else
450 break;
451 }
Ted Kremenekf59bf482008-07-17 18:38:48 +0000452 }
453
454 // Remove dead variable bindings.
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000455 for (BindingsTy::iterator I=B.begin(), E=B.end(); I!=E ; ++I) {
Ted Kremenekf684d562009-03-05 18:08:28 +0000456 const MemRegion* R = I.getKey();
Ted Kremenek9e240492008-10-04 05:50:14 +0000457
458 if (!Marked.count(R)) {
Zhongxing Xud91ee272009-06-23 09:02:15 +0000459 store = Remove(store, ValMgr.makeLoc(R));
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000460 SVal X = I.getData();
Ted Kremenekf59bf482008-07-17 18:38:48 +0000461
462 for (symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); SI!=SE; ++SI)
Ted Kremenek241677a2009-01-21 22:26:05 +0000463 SymReaper.maybeDead(*SI);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000464 }
Ted Kremenek9e240492008-10-04 05:50:14 +0000465 }
Ted Kremenekf684d562009-03-05 18:08:28 +0000466
Ted Kremenek2f26bc32009-08-02 04:45:08 +0000467 // Write the store back.
468 state.setStore(store);
Ted Kremenekf59bf482008-07-17 18:38:48 +0000469}
Ted Kremenekcaa37242008-08-19 16:51:45 +0000470
Ted Kremenekf684d562009-03-05 18:08:28 +0000471Store BasicStoreManager::scanForIvars(Stmt *B, const Decl* SelfDecl, Store St) {
472 for (Stmt::child_iterator CI=B->child_begin(), CE=B->child_end();
473 CI != CE; ++CI) {
474
475 if (!*CI)
476 continue;
477
478 // Check if the statement is an ivar reference. We only
479 // care about self.ivar.
480 if (ObjCIvarRefExpr *IV = dyn_cast<ObjCIvarRefExpr>(*CI)) {
481 const Expr *Base = IV->getBase()->IgnoreParenCasts();
482 if (const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Base)) {
483 if (DR->getDecl() == SelfDecl) {
484 const MemRegion *IVR = MRMgr.getObjCIvarRegion(IV->getDecl(),
Ted Kremenek8d7f5482009-04-09 22:22:44 +0000485 SelfRegion);
Zhongxing Xud9b6ad62009-05-09 04:08:27 +0000486 SVal X = ValMgr.getRegionValueSymbolVal(IVR);
Zhongxing Xud91ee272009-06-23 09:02:15 +0000487 St = BindInternal(St, ValMgr.makeLoc(IVR), X);
Ted Kremenekf684d562009-03-05 18:08:28 +0000488 }
489 }
490 }
491 else
492 St = scanForIvars(*CI, SelfDecl, St);
493 }
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000494
Ted Kremenekf684d562009-03-05 18:08:28 +0000495 return St;
496}
497
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000498Store BasicStoreManager::getInitialStore(const LocationContext *InitLoc) {
Ted Kremenekcaa37242008-08-19 16:51:45 +0000499 // The LiveVariables information already has a compilation of all VarDecls
500 // used in the function. Iterate through this set, and "symbolicate"
501 // any VarDecl whose value originally comes from outside the function.
Ted Kremenekcaa37242008-08-19 16:51:45 +0000502 typedef LiveVariables::AnalysisDataTy LVDataTy;
Zhongxing Xu17fd8632009-08-17 06:19:58 +0000503 LVDataTy& D = InitLoc->getLiveVariables()->getAnalysisData();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000504 Store St = VBFactory.GetEmptyMap().getRoot();
505
506 for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) {
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000507 NamedDecl* ND = const_cast<NamedDecl*>(I->first);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000508
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000509 // Handle implicit parameters.
510 if (ImplicitParamDecl* PD = dyn_cast<ImplicitParamDecl>(ND)) {
Zhongxing Xueda95462009-08-21 02:58:11 +0000511 const Decl& CD = *InitLoc->getDecl();
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000512 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(&CD)) {
513 if (MD->getSelfDecl() == PD) {
514 // Create a region for "self".
515 assert (SelfRegion == 0);
516 SelfRegion = MRMgr.getObjCObjectRegion(MD->getClassInterface(),
517 MRMgr.getHeapRegion());
518
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000519 St = BindInternal(St, ValMgr.makeLoc(MRMgr.getVarRegion(PD, InitLoc)),
Zhongxing Xud91ee272009-06-23 09:02:15 +0000520 ValMgr.makeLoc(SelfRegion));
Ted Kremenekf684d562009-03-05 18:08:28 +0000521
522 // Scan the method for ivar references. While this requires an
523 // entire AST scan, the cost should not be high in practice.
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000524 St = scanForIvars(MD->getBody(), PD, St);
Ted Kremenek9deb0e32008-10-24 20:32:16 +0000525 }
526 }
527 }
528 else if (VarDecl* VD = dyn_cast<VarDecl>(ND)) {
Ted Kremenek693de5d2009-03-23 15:42:58 +0000529 // 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 Kremenekd17da2b2009-08-21 22:28:32 +0000535 const MemRegion *R = ValMgr.getRegionManager().getVarRegion(VD, InitLoc);
Ted Kremenek15086362009-07-02 18:25:09 +0000536 SVal X = R->hasGlobalsOrParametersStorage()
537 ? ValMgr.getRegionValueSymbolVal(R)
538 : UndefinedVal();
Ted Kremenekcaa37242008-08-19 16:51:45 +0000539
Zhongxing Xud91ee272009-06-23 09:02:15 +0000540 St = BindInternal(St, ValMgr.makeLoc(R), X);
Ted Kremenekcaa37242008-08-19 16:51:45 +0000541 }
542 }
543 return St;
544}
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000545
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000546Store BasicStoreManager::BindDeclInternal(Store store, const VarDecl* VD,
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000547 const LocationContext *LC,
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000548 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))
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000578 store = BindInternal(store, getLoc(VD, LC),
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())
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000581 store = BindInternal(store, getLoc(VD, LC),
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 {
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000587 store = BindInternal(store, getLoc(VD, LC), *InitVal);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +0000588 }
589 }
590 } else {
591 // Process local scalar variables.
592 QualType T = VD->getType();
Ted Kremenekf6c9f422009-07-03 00:36:16 +0000593 if (ValMgr.getSymbolManager().canSymbolicate(T)) {
Ted Kremenek42577d12008-11-12 19:18:35 +0000594 SVal V = InitVal ? *InitVal : UndefinedVal();
Ted Kremenekd17da2b2009-08-21 22:28:32 +0000595 store = BindInternal(store, getLoc(VD, LC), 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 Kremenek6f9b3a42009-07-13 23:53:06 +0000616 Out << ' ' << I.getKey() << " : " << I.getData();
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000617 }
618}
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000619
Ted Kremenek60dbad82008-09-03 03:06:11 +0000620
621void BasicStoreManager::iterBindings(Store store, BindingsHandler& f) {
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000622 BindingsTy B = GetBindings(store);
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000623
Ted Kremenek2c8e7882009-03-05 16:32:59 +0000624 for (BindingsTy::iterator I=B.begin(), E=B.end(); I != E; ++I)
Ted Kremenekbe912242009-03-05 16:31:07 +0000625 f.HandleBinding(*this, store, I.getKey(), I.getData());
Ted Kremenek9e240492008-10-04 05:50:14 +0000626
Ted Kremenek2bc39c62008-08-29 00:47:32 +0000627}
628
Ted Kremenek60dbad82008-09-03 03:06:11 +0000629StoreManager::BindingsHandler::~BindingsHandler() {}
Ted Kremenek1004a9f2009-07-29 18:16:25 +0000630
631//===----------------------------------------------------------------------===//
632// Binding invalidation.
633//===----------------------------------------------------------------------===//
634
635const GRState *BasicStoreManager::InvalidateRegion(const GRState *state,
636 const MemRegion *R,
637 const Expr *E,
638 unsigned Count) {
639 R = R->getBaseRegion();
640
641 if (!(isa<VarRegion>(R) || isa<ObjCIvarRegion>(R)))
642 return state;
643
644 // We only track bindings to self.ivar.
645 if (const ObjCIvarRegion *IVR = dyn_cast<ObjCIvarRegion>(R))
646 if (IVR->getSuperRegion() != SelfRegion)
647 return state;
648
649 QualType T = cast<TypedRegion>(R)->getValueType(R->getContext());
650 SVal V = ValMgr.getConjuredSymbolVal(E, T, Count);
651 return Bind(state, loc::MemRegionVal(R), V);
652}
653